Public · Protected · Private
Interesting C# 2
Type: Public  |  Created: 2008-08-20  |  Frozen: Yes
« Previous Public Blog Next Public Blog »
Comments
  • 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");
    2008-08-20 23:34
  • public string Name { get { lock( this ) { return _name; } } set { lock( this ) { _name = value; } } } Multithreading for properties is easy to implement
    2008-08-20 23:49
  • Imagine access control on properties public class Customer { private string _name; public virtual string Name { get { return _name; } protected set { _name = value; } } }
    2008-08-20 23:53
  • // 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;
    2008-08-21 00:08
  • 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;
    2008-08-21 00:20
  • 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.
    2008-08-21 08:20
This blog is frozen. No new comments or edits allowed.