0

I understand that a string is sent by value even though it is a reference type, but if my string was tens of MB in size and i wanted to send it as an argument.

should i send it by reference or value?

private int GetIndexOfNext(string String,int SearchStartIndex,char TargetChar)

Or

private int GetIndexOfNext(ref string String,int SearchStartIndex,char TargetChar)
FPGA
  • 3,525
  • 10
  • 44
  • 73
  • To save memory I would send byref as it passes the same string instance without creating new memory allocation as with byval. – xspydr Jan 31 '14 at 18:49
  • Choose whichever you like. not important. Compiler will **not** pass a copy of it. – L.B Jan 31 '14 at 18:49
  • 1
    "I understand that a string is sent by value even though it is a reference type" - Excuse me? It is possible that you just express yourself ambigiously but this hints at a fatal misunderstanding. I'll try to state this as clearly as possible: Passing a string around does *not* create a copy of the string object. –  Jan 31 '14 at 18:51
  • 1
    possible duplicate of [In C#, why is String a reference type that behaves like a value type?](http://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type) - you "understand it wrong". Please read the linked topic. – quetzalcoatl Jan 31 '14 at 18:52

3 Answers3

5

I understand that a string is sent by value even though it is a reference type

For strings or other reference types, their address is passed by value. It is not the value which gets passed, so for your case it doesn't matter. The reason parameter passing involving strings looks different is because strings are immutable (and that when you try to modify string's content).

You should see: Parameter passing in C# by Jon Skeet

Habib
  • 219,104
  • 29
  • 407
  • 436
3

It's fine to pass it as a string without the ref. The string isn't copied, you've just passed a copy of the reference to the method, rather than a copy of the string.

Sean
  • 60,939
  • 11
  • 97
  • 136
0

A small (unsafe) test will show that it's the string address that is passed and not a copy of the string.

    static void Main(string[] args)
    {
        String s = "aaa";

        Console.WriteLine(s); // Prints aaa
        F(s);
        Console.WriteLine(s); // Prints aba
        Console.ReadLine();
    }

    static unsafe void F(String s)
    {
        fixed (char* p = s)
        {
            p[1] = 'b';
        }
    }
the_lotus
  • 12,668
  • 3
  • 36
  • 53
  • All parameters in C# are passed by value by default, in this case the value is itself a reference. So the object is not passed by reference, rather it is a reference that is passed by value. – Servy Jan 31 '14 at 19:03
  • @Servy That's what I mean, I was just using the terminology of his question which might not be a good idea. – the_lotus Jan 31 '14 at 19:05
  • The statement `Strings aren't passed by value` is incorrect, or at the very best, entirely unclear as to its meaning. – Servy Jan 31 '14 at 19:06