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,
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 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.
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′).
The member function assign() is used for the assignments, it assigns a new value to the string, replacing its current contents.
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With