0

Just trying to see if someone can help me understand this:

What is the difference in usage between these two examples below? When is the second, passing an argument by reference better? Both output the same result.

public static void Sqr(int x)
{
    x = x * x;
    Console.WriteLine(x);
}

static void Main(string[] args)
{
    Sqr(3);
}

2)

public static void Sqr(ref int x)
{
    x = x * x;
}

static void Main(string[] args)
{
    int a = 3;
    Sqr(ref a);
    Console.WriteLine(a);
}

Anyone know? I’m clueless here. My problem is that don’t really even know why things are written the way they are!! Any insights?

lalli
  • 6,083
  • 7
  • 42
  • 55
  • 1
    Does this answer your question? [Why use the 'ref' keyword when passing an object?](https://stackoverflow.com/questions/186891/why-use-the-ref-keyword-when-passing-an-object) – Arthur Attout Nov 08 '19 at 07:36
  • 1
    Fun experiment: try the first implementation of `Sqr` in the second implementation of `Main` (you need to remove the `ref` in the call to `Sqr(ref a)`). Keep everything else exactly the same. Run it. There should be an output of two lines. The first would be from the inside of the `Sqr` method, the second would be from inside the `Main` method, _after_ the call to `Sqr`. Compare those two outputs and reason about _why_ they are the way they are. Especially when compared to only the second implementation. – Corak Nov 08 '19 at 07:48
  • See https://jonskeet.uk/csharp/parameters.html – Jon Skeet Nov 08 '19 at 08:27

1 Answers1

2

Both output the same result.

That's because as well as changing whether or not it's a ref parameter, you've changed where you've got the output.

Here's a version of your first piece of code that is closer to the second, so they only differ in whether or not it's a ref parameter:

public static void Sqr(int x)
{
    x = x * x;
}

static void Main(string[] args)
{
    int a = 3;
    Sqr(a);
    Console.WriteLine(a);
}

Now the output will be 3, because the value of a hasn't changed - its value was passed to the Sqr method. In your second example, the variable itself is passed by reference, so x in the Sqr method and a in the Main method refer to the same storage location... the change to the value of x can be "seen" via a after the method has returned.

When is the second, passing an argument by reference better?

When you want changes in the parameter to be reflected in the argument. That's relatively rare, and I'd encourage you to use value parameters by default. For example, it would be more idiomatic to write Sqr as:

public static int Sqr(int x)
{
    return x * x;
}

Then call it as:

a = Sqr(a);

ref parameters are relatively rarely useful in my experience - but it's important to understand how they work. (You may find my article on parameter passing useful for more details.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thank you Jon for your detailed answer. I think your answer is passing a method by value? (don’t really know what is technically happening) In your example, what is the point of x= x* x if it’s not going to be used? – Yoshi Onimusha Nov 09 '19 at 10:49
  • @YoshiOnimusha: It's not passing a method at all. My point is that the first method *is* pointless, precisely because the argument is passed by value... and you can see that it has no effect when you make the two examples similar in terms of how they're printing the result. – Jon Skeet Nov 09 '19 at 15:06