Interfaces in C#
Can properties be defined in an interface?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ajay YadavPosted Oct 28, 2014, 11:29 PM
Abhishek KumarPosted Sep 30, 2014, 5:04 AM
Syntax.
public interface Isample
{
string name
{
get;
set;
}
}
Abhishek
Munesh SharmaPosted Sep 30, 2014, 2:45 AM
Saineshwar BageriPosted Sep 30, 2014, 12:22 AM
public interface IResourcePolicy
{
string Version { get; set; }
}
In the implementing class, you need to implement it:
public class ResourcePolicy : IResourcePolicy
{
string Version { get; set; }
}
This looks similar, but it is something completely different.
In the interface, there is no code, you just specify the there is a property with a getter and a setter, whatever they will do.
In the class, you actually implement them.
The shortest way to do this is using this { get; set; } syntax,
the compiler will create a field and generate the getter and setter implementation for it.