首页 > 其他 > 详细

【转载】#443 - An Interface Cannot Contain Fields

时间:2014-03-13 05:10:10      阅读:548      评论:0      收藏:0      [点我收藏+]

An interface can contain methods, properties, events or indexers. It cannot contain fields.

bubuko.com,布布扣
1 interface IMoo
2 {
3     // Methods
4     void Moo();
5 
6     // Field not allow - compile-time error
7     string Name;
8 }
bubuko.com,布布扣

Instead of a field, you can use a property.

bubuko.com,布布扣
1 interface IMoo
2 {
3      // Methods
4      void Moo();
5  
6      // Name as a property is OK
7      string Name{get; set;}
8 }
bubuko.com,布布扣

Interfaces don‘t allow fields because they consist of a constract that is a list of methods, whose implementation is provided by a class. Properties are implemented as methods (get and set accessors), so they fit this model. But fields are just data locations, so it doesn‘t make sense to include them in an interface.

原文地址:#443 - An Interface Cannot Contain Fields

【转载】#443 - An Interface Cannot Contain Fields,布布扣,bubuko.com

【转载】#443 - An Interface Cannot Contain Fields

原文:http://www.cnblogs.com/yuthreestone/p/3596722.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!