您的位置:首页 > 大数据 > 人工智能

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

2014-03-12 16:59 423 查看
An interface can contain methods, properties, events or indexers. It cannot contain fields.

interface IMoo
{
// Methods
void Moo();

// Field not allow - compile-time error
string Name;
}


Instead of a field, you can use a property.

interface IMoo
{
// Methods
void Moo();

// Name as a property is OK
string Name{get; set;}
}


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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐