Know the answer? Post it. Somebody with the same question will find it here.
Munesh SharmaRank #174 · 13.9K reputationPosted Oct 8, 2015, 12:51 AM EDT
0
If an interface is a collection of slots that contain methods, then some of those slots can also contain the get and set methods of a property, the get and set methods of an indexer, and the add and remove methods of an event. But a field is not a method. There's no "slot" associated with a field that you can then "fill in" with a reference to the field location. And therefore, interfaces can define methods, properties, indexers and events, but not fields.
0
Priyaranjan K SRank #45 · 32.5K reputationPosted Oct 8, 2015, 1:00 AM EDT
0
Hi Dev ,
Interface is like an agreement or better say Contract that asks the Class that implements the interface to define all the methods within the interface . Now methods within the interface can be defined inside the class by associating a logic for that class , but a field cannot be defined or implemented/assigned a value within the class . However value assignments can be done using Properties because they have a getter/setter method as below :
interface IEmployee
{
string Name
{
get;
set;
}
}
public class Employee : IEmployee
{
private string name;
public string Name // read-write instance property
{
get
{
return name;
}
set
{
name = value;
}
}
}
Thanks,
Priyan
If you found this useful Please mark this as Answer
0
Mukesh KumarRank #119 · 18.2K reputationPosted Oct 8, 2015, 1:33 AM EDT
0
Hi,
Though many of the other answers are correct at the semantic level, I find it interesting to also approach these sorts of questions from the implementation details level.
An interface can be thought of as a collection of slots, which contain methods. When a class implements an interface, the class is required to tell the runtime how to fill in all the required slots. When you say
the class says "when you create an instance of me, stuff a reference to Foo.M in the slot for IFoo.M.
Then when you do a call:
Code
IFoo ifoo = new Foo(); ifoo.M();
the compiler generates code that says "ask the object what method is in the slot for IFoo.M, and call that method.
If an interface is a collection of slots that contain methods, then some of those slots can also contain the get and set methods of a property, the get and set methods of an indexer, and the add and remove methods of an event. But a field is not a method. There's no "slot" associated with a field that you can then "fill in" with a reference to the field location. And therefore, interfaces can define methods, properties, indexers and events, but not fields.
Munesh SharmaRank #174 · 13.9K reputationPosted Oct 8, 2015, 12:51 AM EDT
Priyaranjan K SRank #45 · 32.5K reputationPosted Oct 8, 2015, 1:00 AM EDT
Mukesh KumarRank #119 · 18.2K reputationPosted Oct 8, 2015, 1:33 AM EDT
Though many of the other answers are correct at the semantic level, I find it interesting to also approach these sorts of questions from the implementation details level.
An interface can be thought of as a collection of slots, which contain methods. When a class implements an interface, the class is required to tell the runtime how to fill in all the required slots. When you say
interface IFoo { void M(); } class Foo : IFoo { public void M() { ... } }the class says "when you create an instance of me, stuff a reference to Foo.M in the slot for IFoo.M.
Then when you do a call:
the compiler generates code that says "ask the object what method is in the slot for IFoo.M, and call that method.
If an interface is a collection of slots that contain methods, then some of those slots can also contain the get and set methods of a property, the get and set methods of an indexer, and the add and remove methods of an event. But a field is not a method. There's no "slot" associated with a field that you can then "fill in" with a reference to the field location. And therefore, interfaces can define methods, properties, indexers and events, but not fields.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts