开发者

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

开发者 https://www.devze.com 2022-12-15 01:44 出处:网络
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

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

精彩评论

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

关注公众号