Interesting C# 2

Do you like this code
public class Address
{
public string Name;
public string City;
}

intead of
public class Address
{
private string _name;
public string Name
{
get{return _name;}
set{ _name = value;}
}
private string _city;
public string City
{
get { return _city; }
set { _city = value; }
}
}

Here is one benifit...

Address aa = new Address();
aa.Name = "hero";
aa.City = "bombay";
textBox1.DataBindings.Add("Text", aa, "City");
public string Name
{
get { lock( this )
{ return _name; }
}
set { lock( this )
{ _name = value; }
}
}
Multithreading for properties is easy to implement
Imagine access control on properties
public class Customer
{
private string _name;
public virtual string Name
{
get
{
return _name;
}
protected set
{
_name = value;
}
}
}
// Compile time constant: high-speed - can't change
public const int A = 20;

// Runtime constant: not very fast but flexible to change
public static readonly int A = 24;
public class Address
{
private string _name;
public string Name
{
get{return _name;}
set{ _name = value;}
}
private string _city;
public string City
{
get { return _city; }
set { _city = value; }
}
public static implicit operator MyType(Address s )
{
return new MyType(s.City ,s.Name ) ;
}
}
public class MyType
{
public MyType(string h, string j)
{
}
}

now you can cast directly
Address aa = new Address();
aa.Name = ""; aa.City = "bombay";

MyType m = aa;
Immutable types are inherently thread safe: Multiple readers can access the same contents. If the internal state cannot change, there is no chance for different threads to see inconsistent views of the data. Immutable types can be exported from your objects safely.