简体   繁体   中英

How a String type get Passed to a Method or Assigned to a Variable in C#?

String object behaves like a Value type when using == and != operators which means the actual object rather than the reference is checked.

What about parameter passing, assignments and copying?

String Parameter Passing: When a reference type is passed to a method, its reference is copied but the underlying object stays the same.

Would this also be true about String type in C#? I mean would there be 2 pointers (messageVar and messageParam) pointing to the same object in the below code:

public static void main()
{
   string messageVar = "C#";
   Test(messageVar);

   // what about in assignement?
   string messageVar2 = messageVar;
}
public void Test(string messageParam)
{
// logic
}

What about when it's assigned to a variable? I'd guess, the reference would be copied only and the actual object stays the same cached in the String Intern Pool. Not sure.

Would messageVar2 also refers to the same object?

Thanks,

Yes, you are correct only references are copied, the object instance referenced is one and the same.

You can easily verify this using Object.ReferenceEquals() to test if two references point to the same object instance - I modified your example a bit to show this:

static string messageVar = "C#";
public static void Main(string[] args)
{
    bool isSame = Test(messageVar); //true

    // what about in assignement?
    string messageVar2 = messageVar;
    isSame = Object.ReferenceEquals(messageVar2, messageVar);//also true
}

public static bool Test(string messageParam)
{
    // logic
    bool isSame = Object.ReferenceEquals(messageParam, messageVar);
    return isSame;
}

“String object behaves like a Value type when using == and != operators which means the actual object rather than the reference is checked.”

The String class does not get any special treatment (at a language level) in this regard. The reason that the == and != operators compare the values (rather than the references) of strings is because they have been overloaded ; see String.Equality Operator and String.Inequality Operator .

Any user-defined class may also overload these two operators to perform equality comparisons on object values (although this is typically only recommended for immutable types, such as String ).

That's exactly what happens.

The little twist with string s is that they're immutable: any operation that looks like it's modifying the string actually creates a new one. So strings cannot be changed once they're created. Aside from that, strings work like any other reference type: they're accessed by reference, and passing them to a function passes a copy of the reference (unless you use ref or out ), so modifying the reference in the function does not affect the value in the calling code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM