Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 235
The Light Avatar asked Feb 11 '12 16:02

The Light


People also ask

Can you assign a string to a variable in C?

C has very little syntactical support for strings. There are no string operators (only char-array and char-pointer operators). You can't assign strings.

How do you assign a string value to a variable?

How to create a string and assign it to a variable. To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and then assign it to a variable.

How are strings passed in C?

As a whole, both lines mean it is just a string of characters that uses an array of char variables for storing. A string is enclosed in a double quotation mark (i.e., “ijklm”). Unlike a character enclosed in a single quotation mark per character (i.e. 'i', 'j',…'\0′).

Which function is assign string to variable?

The member function assign() is used for the assignments, it assigns a new value to the string, replacing its current contents.


2 Answers

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;
}
like image 190
BrokenGlass Avatar answered Sep 25 '22 16:09

BrokenGlass


“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).

like image 23
Douglas Avatar answered Sep 23 '22 16:09

Douglas