Why System.String's beahaves like a value type?
Posted
by Sorush Rabiee
on Stack Overflow
See other posts from Stack Overflow
or by Sorush Rabiee
Published on 2010-05-25T11:59:05Z
Indexed on
2010/05/25
12:01 UTC
Read the original article
Hit count: 123
I want to write a 'Date' class that behaves like a Value Type. for example, Instead of writing a Clone method for setting properties safely, make the Date class to pass by value:
public Date Birthday
{
get { return this.birthday; }
set
{
this.birthday = value.Clone();
} //I want to write this.birthday = value;
//without changing external value when this.Birthday changes
}
I know this is possible because System.String is a class and behaves like a value. for example:
String s1 = "Hello";
String s2 = "Hi";
s1 = s2;
s2="Hello";
Console.WriteLine(s1); //Prints 'Hi'
First I thought writers of this class override '=' operator, but now I know that the '=' operator can not be overridden. so how they write String class?
© Stack Overflow or respective owner