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

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

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

Instead of a field, you can use a property.

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

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

转载于:https://www.cnblogs.com/yuthreestone/p/3596722.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值