c# - 接口(interface)可以在 C# 中有静态变量吗

标签 c# .net oop c#-4.0 interface

这可能是一个愚蠢的问题,如果有人能帮助我理解它,我将不胜感激。

  1. C# 中的接口(interface)可以有静态变量吗?

  2. 如果接口(interface)本身需要是static的才能在里面声明静态变量?

  3. 当我们在类中实现时,如何实现接口(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/

相关文章:

c# - struct Cat(string, string, bool, bool)' 必须声明一个主体,因为它没有标记为 abstract、extern 或 partial

c# - 创建 Enumerable<KeyValuePair<TKey, TValue>> 的扩展方法 TryGetValue(TKey, out TValue) 就像 Dictionary<TKey, TValue> 一样

c# - 最快的 GDI+ 渲染设置是什么?

PHP OOP 分页

c#泛型类工厂问题

c# - C#如何打开一个Word文件并设置相对于项目文件夹的路径

c# - 为什么 Delta.Patch 不会将字节数组更新为空值?

c# - 从列表中获取值

c# - OOP 设计以及列表和集合 (C#)

python - 我怎样才能简单地从现有实例继承方法?