Interfaces in C#
Can properties be defined in an interface?
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Saineshwar BageriRank #115 · 19.6K reputationPosted Sep 29, 2014, 8:22 PM EDT
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.
Munesh SharmaRank #174 · 13.9K reputationPosted Sep 29, 2014, 10:45 PM EDT
Abhishek KumarRank #510 · 4.8K reputationPosted Sep 30, 2014, 1:04 AM EDT
Syntax.
public interface Isample
{
string name
{
get;
set;
}
}
Abhishek
Ajay YadavRank #313 · 8.1K reputationPosted Oct 28, 2014, 7:29 PM EDT