开发者

String is reference type

String is immutable reference type how?. prove it 开发者_如何学JAVA?


You can always check the ECMA C# Language Specification:

8.2.1 Predefined types
C# provides a set of predefined types, most of which will be familiar to C and C++ developers. The predefined reference types are object and string. The type object is the ultimate base type of all other types. The type string is used to represent Unicode string values. Values of type string are immutable.


Prove that string (if you mean System.String of course) is reference type is quite easy.

All value types should inherited from ValueType (implicitly of caurse), but System.String inherited directly from System.Object, so with elimination approach string can't be anything other that reference type.

Console.WriteLine("".GetType().BaseType); //prints System.Object
Console.WriteLine(1.GetType().BaseType); //prints System.ValueType

To check immutability you should find any "mutating" method inside System.String class. I can't find any! Because all "mutating" method actually returns another instance and you could easily check this by calling object.ReferenceEquals:

Console.WriteLine(object.ReferenceEquals(s, s + "1")); //False
Console.WriteLine(object.ReferenceEquals(s, s.Insert(0, "12"))); //False

You may also check another methods and you'll see that all of they have the same behavior: they return new object, but they not mutate existing values.

From class System.String documentation:

Immutability and the StringBuilder Class

A String object is called immutable (read-only), because its value cannot be modified after it has been created. Methods that appear to modify a String object actually return a new String object that contains the modification.


Proving is simply a matter of going through every single externally-accessible (public, protected, internal or protected internal) member and seeing if they can mutate the object. None of them can, hence it is immutable.

The how is easy:

Immutability of a reference type enforced member definitions:

public class TestClass
{
  private readonly int _someValue;
  public TestClass(int val){_someValue = val;}
  public int Value{get{return _someValue;}}
}

Immutability enforced by interface alone:

public class TestClass
{
  private int _someValue;
  public TestClass(int val){_someValue = val;}
  public int Value{get{return _someValue;}}
}

Both of these classes define immutable reference types. The first is both internally and externally immutable the second is internally mutable, but externally immutable, as no externally accessible method can change the value of any instance member.

String is in the second case incidentally, though that's an implementation detail.


Here's something to read about that: http://msdn.microsoft.com/en-us/library/362314fe.aspx


Why string is immutable and StringBuilder is mutable ?

string builder is mutable .Ie it is pure reference type.The following example the 'foo' is passed as a reference type .So the output is helloanish: E.g:

class Program
{
static void Main(string[] args)
{
StringBuilder y =new StringBuilder();
y.Append("hello");

Program p = new Program();
p.foo( y);
Console.WriteLine(y );
}

void foo(StringBuilder sb)
{
sb .Append("anish:");
}
}

Stirng is immutable .In some ways it to be value types .These are known as immutable .The following example the 'foo' is passed is a string but this act as a value type .So the output is 'hello'

E.g:

class Program
{
static void Main(string[] args)
{
string y =string.Empty;
y="hello";

Program p = new Program();
p.foo( y);
Console.WriteLine(y );
}

void foo(string sb)
{
sb +="anish:";
}
}

Note : note only string .Some of the other types also.

Rather than creating a new storage location for the function member declaration,the same storage location can be used with the help of ref type.

E.g :

class Program
{
static void Main(string[] args)
{
string y ="";
y="hello";

Program p = new Program();
p.foo(ref y);
Console.WriteLine(y );
}

void foo(ref string sb)
{
sb="anish:";
}
}

Output is not hello,its anish

Out type :Same a ref .The thing is that initially the variable is unassigned.

have a look on my blog:http: http://anishmarokey.blogspot.com/2009/08/mutable-vs-immutable.html


String is a reference type in C#. When we create variable of type string, one object of type string gets created in memory. This object cannot be modified once created.

If we assign some other value to same string variable, new object gets created in memory and string variable points to this new address of object.

That is why string is considered as a immutable type.

As Ruel said, you can get more information about string type here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜