1

I can't understand this in C#, despite looking for similar questions and answers.

I have an object cCar (class Car) with ID = 0. I passed this object to a method called CreateCarInDB from my class CarManager without using the ref word :

public static void CreateCarInDB(Car p_cCar) 
{
    int newId = CarDB.SaveNewCar(p_cCar);
    p_cCar.ID = new Id;
}

I just want to understand why after executing the method, if i look, the cCar object has its Id = 1 for example. Shouldn't I use the word ref before method parameter so that this value is affected outside the method? I thought I was only passing the value and not the reference.

It doesn't seem I need to use the word ref.

  • 1
    In C# every object is, by default, passed by reference. Keyword ref is mostly used to send value types (int, double, ...) by reference. – Goran Ćojanović Sep 28 '18 at 09:18
  • 1
    @GoranĆojanović: No, that's not true. Objects aren't passed at all. References are - and they're passed by value by default. It's a very important distinction. – Jon Skeet Sep 28 '18 at 09:18
  • See also: https://stackoverflow.com/a/186907/2085502 – Christoph Lütjen Sep 28 '18 at 09:19
  • @JonSkeet I know, but such explanation could be confusing for someone new in C#. – Goran Ćojanović Sep 28 '18 at 09:20
  • @GoranĆojanović: I believe quite the opposite - I believe that it's reasonably easy to explain it precisely, but that muddling the two (understanding what a reference is vs pass-by-reference) can cause a *lot* of confusion. In your model of "objects are passed by reference" what happens with simple assigning? `Car x = new Car(); Car y = x; y.Make = "Ford";`? There's no "passing" there - so you have to fudge things again. With the correct model (the values of `x` and `y` are just references, not objects) no fudging is required, and you can be consistent throughout. – Jon Skeet Sep 28 '18 at 09:22
  • As an aside, I'd strongly advise against naming conventions like "p_c" etc. See https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/general-naming-conventions. This has nothing to do with pass-by-reference etc, but it's good to get into the habit of following the platform conventions as early as possible. – Jon Skeet Sep 28 '18 at 09:22
  • @JonSkeet I agree on your explanation and I agree with you for the naming conventions, I almost never use underscore, my bad. – Nicolas Daumalle Sep 28 '18 at 09:31
  • Is it ok if I use the ref word anyway just to remind me that some properties are changed in method? – Nicolas Daumalle Sep 28 '18 at 09:32
  • No, that's misleading - it suggests that you might change which object is being referred to. In some cases that would also make it more inconvenient to call. (You couldn't pass in the result of a method call or a property for example - you'd have to assign to a local variable, and pass that by reference.) – Jon Skeet Sep 28 '18 at 09:38

1 Answers1

4

Shouldn't I use the word ref before method parameter so that this value is affected outside the method?

No, because you're not changing the value of p_cCar, which is just a reference. You're changing the content of the object that the reference refers to.

If you had a line of code like this:

p_cCar = new Car();

... and you expected the caller to see that change, then you'd need the ref (or out) keyword. That's rarely what you want to do though, in my experience.

To put it in a real-world way, someone can write their address on a piece of paper and ask you to paint their front door green. You can modify the appearance of their house (painting the front door) and they'll see that. But if you decide to cross out their address on the piece of paper and write a different one, that won't change their idea of where they live.

Further information:

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194