这可能是一个愚蠢的问题,如果有人能帮助我理解它,我将不胜感激。
C#
中的接口(interface)可以有静态变量吗?如果接口(interface)本身需要是static的才能在里面声明静态变量?
当我们在类中实现时,如何实现接口(interface)中的静态变量(或者说属性)?
一些例子和清楚的解释将不胜感激。
最佳答案
不,C# 中的接口(interface)根本不能声明字段。您根本不能在 C# 中声明静态接口(interface),也不能在接口(interface)内声明静态成员。
根据 C# 规范的第 11.2 节:
An interface declaration may declare zero or more members. The members of an interface must be methods, properties, events, or indexers. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types, nor can an interface contain static members of any kind.
All interface members implicitly have public access. It is a compile-time error for interface member declarations to include any modifiers. In particular, interfaces members cannot be declared with the modifiers abstract, public, protected, internal, private, virtual, override, or static.
关于c# - 接口(interface)可以在 C# 中有静态变量吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19496157/