开发者

Can we add variables and properties in interfaces in C#.NET?

I want to know that how to add variables (i.e. with which access specifier) in i开发者_如何转开发nterfaces and also can we write property in interfaces in C#.net?


This should have been easy to find on the internet.

Interfaces are contracts to be fulfilled by implementing classes. Hence they can consist of public methods, properties and events (indexers are permitted too).

Variables in Interfaces - NO. Can you elaborate on why you need them? You can have variables in Base classes though.
Properties in Interfaces - Yes, since they are paired methods under the hood.
Members of an interface are implicitly public. You cannot specify access modifiers explicitly

public interface ISampleInterface
{
    // method declaration
    bool CheckSomething(object o);

    // event declaration
    event EventHandler ShapeChanged;

    // Property declaration:
    string Name
    {
        get;
        set;
    }
}

See also

  • Interfaces
  • Interface properties
  • Interface events


Variables in interfaces, I don't think so but I'm not 100% certain?

And yes, you can have properties in interfaces. See the MSDN reference:
Interface Properties (C# Programming Guide)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜