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

I want to know how to add variables (i.e. with which access specifier) ​​to interfaces, and also write a property in interfaces in C # .net?

+7
c #
source share
2 answers

It should have been easy to find on the Internet.

Interfaces are contracts that must be executed by implementing classes. Therefore, they can consist of public methods, properties, and events (indexes are also allowed).

The variables in the interfaces are NO. Can you talk about why they are needed? You can have variables in base classes. Properties in the interfaces. Yes, since they are paired methods under the hood.
Interface members are implicitly public. You cannot explicitly specify access modifiers

public interface ISampleInterface { // method declaration bool CheckSomething(object o); // event declaration event EventHandler ShapeChanged; // Property declaration: string Name { get; set; } } 

see also

+12
source share

Variables in interfaces, I don’t think so, but am I not 100% sure?

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

+1
source share

All Articles