C# 3.0 的自动属性(Automatic Properties)
香水坏坏 发表于 2007-9-17 [ASP.NET]
C#代码
- class Program
- {
- static void Main(string[] args)
- {
- Test1 a = new Test1();
- //incorrectness
- //we would catch this exception
- //"The property or indexer 'ConsoleApplication1.Test1.PropertyB' cannot be used in this context because the set accessor is inaccessible"
- //when build project
- //a.PropertyB = "b";
- //correct
- a.PropertyA = "a";
- //string c = a.PropertyC;
- }
- }
- //c# 3.0+
- class Test1
- {
- //common property type
- public string PropertyA { get; set; }
- //just visiable for all other classes
- public string PropertyB { get; private set; }
- //this are not allowed
- //instead using a field like: string fieldC
- //public string PropertyC { private get;private set;}
- //writable for child class and visiable for all classes;
- public string PropertyD { get; protected set; }
- //justlin visiable for self child
- protected string PropertyE { get; private set; }
- }
相关文章
.net 3.0 C# 新特性http://www.aspstat.com/68
681 3 标签:C#
访客评论
发表评论
- 你的姓名:
- 你的网站:
- EMAIL:
- 评论内容:
- 私人

