2

I came accross two Solutions(both work):

 public List<Label> foo1(ref ISomeInterface[] all)

or

 public List<Label> foo2(ISomeInterface[] all)

Is there a diffrerence, does it matter which of them I take ? Interface is a reference value and will give the parameter as reference anyway and "ref" will also get the reference...I think I can dismiss "ref" ... I wonder why the compiler does not give me an error...

user254197
  • 883
  • 2
  • 9
  • 31

3 Answers3

3

Is there a diffrerence?

Yes, there is. Everything in C# is passed by value. When you pass a reference type by ref, you pass the actual reference pointer rather then a copy. That way, if you pass a reference type by ref and set it to a new reference via the new keyword, you'll alter the reference.

An example:

public static void Main(string[] args)
{
    ISomeInterface[] somes = new[] { new SomeConcreteType() }
    Foo(somes);
    Console.WriteLine(somes.Length) // Will print 1
    Foo(ref somes);
    Console.WriteLine(somes.Length) // Will print 0
}

public List<Label> Foo(ref ISomeInterface[] all)
{
    all = new ISomeInterface[0];
}
public List<Label> Foo(ISomeInterface[] all)
{
    all = new ISomeInterface[0];
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
2

In first case you replace "global" (out of method) parameter all. In second case you will replace local copy of all parameter.

public List<Label> foo1(ref ISomeInterface[] all)
{
    all = new ISomeInterface[0]; //you will get empty array outside method
}

public List<Label> foo1(ISomeInterface[] all)
{
    all = new ISomeInterface[0]; //you will get empty array only inside method
}
Backs
  • 24,430
  • 5
  • 58
  • 85
  • Just a small question from my side: Is it always "call by value" in C# even for arrays? I mean that just sounds unusual, but possible. It would mean it needs to copy the whole array to the stack on function call without ref. – Matthias Aug 10 '15 at 08:02
  • 1
    @Matthias id always call by value. But it woun't copy an array, it'll copy just reference (pointer) to array. Here, value is just a small pointer – Backs Aug 10 '15 at 08:03
  • OK. So it is just about the pointer not the payload. – Matthias Aug 10 '15 at 08:05
  • I think that if it does not copy the array it should be called "call by reference"? – Matthias Aug 10 '15 at 08:06
  • 2
    @Matthias no, it seems, but no. Here is a pointer. And his value is address of array. And when you call without `ref` you pass value and copy it to local pointer with the same value (address of the same array). You can modify this local pointer (with `new`) and it does not affect another varibale with the same value (address of array). When you pass ny `ref` you have no additional local variables. And this is "call by reference" – Backs Aug 10 '15 at 08:11
0

It depends on what you want to do with the array. If you want to modify the value in the foo1 method and use those modifications outside of the foo1 method, you may want to use the ref type version

If you just want to use the returned List<Label> you should use the option without ref.