C# 3.0 的自动属性(Automatic Properties)

香水坏坏 发表于 2007-9-17 [ASP.NET]
C#代码
  1. class Program   
  2. {   
  3.     static void Main(string[] args)   
  4.     {   
  5.         Test1 a = new Test1();   
  6.         //incorrectness   
  7.         //we would catch this exception    
  8.         //"The property or indexer 'ConsoleApplication1.Test1.PropertyB' cannot be used in this context because the set accessor is inaccessible"   
  9.         //when build project   
  10.         //a.PropertyB = "b";   
  11.   
  12.         //correct   
  13.         a.PropertyA = "a";   
  14.   
  15.         //string c = a.PropertyC;   
  16.     }   
  17. }   
  18.   
  19. //c# 3.0+   
  20. class Test1   
  21. {   
  22.     //common property type   
  23.     public string PropertyA { getset; }   
  24.     //just visiable for all other classes   
  25.     public string PropertyB { getprivate set; }    
  26.       
  27.     //this are not allowed   
  28.     //instead using a field like: string fieldC   
  29.     //public string PropertyC { private get;private set;}   
  30.   
  31.     //writable for child class and visiable for all classes;   
  32.     public string PropertyD { getprotected set; }   
  33.   
  34.     //justlin visiable for self child   
  35.     protected string PropertyE { getprivate set; }   
  36.   
  37. }   

 

相关文章

.net 3.0 C# 新特性http://www.aspstat.com/68

681 3 标签:C# 
访客评论
  • 香水坏坏    [2007-9-19]
    看了人家的文字和照片,大伙也该留个言吧 嘿嘿...
  • 香水坏坏    [2007-9-19]
    5555555555 木时间木状态呀
  • llinzzi    [2007-9-19]
    汗,早用上了,更新点特别的呀 :)
发表评论
  • 你的姓名:
  • 你的网站:
  •   EMAIL:   
  • 评论内容:
  • 私人