Friday, 3 February 2012

All You'd Need to Know About String Type in C#

  • string is an alias for the String object which means they are equivalent.
  • String object is an Immutable Reference Type because it inherits directly from System.Object.
  • String object is not a Value Type because it doesn't inherit from System.ValueType (although the ValueType itself inherits from System.Object ultimately) but behaves like a Value Type.
  • A String object is internally stored as a Read-Only Collection of Char Objects.
What Does it Mean "String Object is Immutable"?
It simply means that once the object is defined, it can not be changed internally. If you change its value once initialized, then a new String Object will be created and the old one becomes ready for Garbage Collection.

In What Way a String Object Behaves Like a Value Type?
  1. Equality Operators "==", compares the value within the 2 String Objects like Value Types and unlike other Reference Types.
How was it achieved?
Well, you can override any operator that you like in classes including the Equality and Inequality operators. This is how:
public static bool operator ==(
    string a,
    string b
)
public static bool operator !=(
    string a,
    string b
)

In What Way a String Object Behaves like a Reference Type?
It has a reference and an actual object.
  1. Parameter Passing: When you pass a String object to a method as an input parameter, its reference is copied and passed while the object stays the same (in the String Intern Pool).
  2. In assignments, It makes a copy of the pointer and points to the same object in the String Intern Pool. String.Clone() also just returns a reference to the same String Object.
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;
}
What is String Intern Pool?
Where String objects are cached to achieve better memory management.
http://msdn.microsoft.com/en-us/library/system.string.intern.aspx

References:
http://msdn.microsoft.com/en-us/library/362314fe(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/ms228362.aspx

2 comments:

Concrete Gannet said...

In What Way a String Object Behaves Like a Value Type?
Hi Pooya,

You are right that *in the C# porgramming language*, == and != for strings have been overloaded and behave differently from other .NET reference types. In my view that is a design mistake, but one we have to understand and live with.

I do not believe you are correct
re parameter passing, assignment and copying. In all these cases, you are using a *reference* to an object, that happens to be immutable. That does *not* mean the string behaves like a value type. Do you have any evidence for 2, 3 and 4?

Pooya Khamooshi said...

Thanks for your comment. I believe you're correct. the string value is stored in the String Intern Pool and just the pointer is copied in assignments and parameter passings.