- 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.
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?
- 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:
In What Way a String Object Behaves like a Reference Type?
It has a reference and an actual object.Where String objects are cached to achieve better memory management.
- 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).
- 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.
What is String Intern Pool?