Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Property Clarification

According to the definition :

"As interface is not an object by itself ,I can't initialize it.If interface were allowed to declare fileds, then it needs storage location,so we can not declare fields inside interface."

Incase of property say example

when i declare

 string SayHello { get; set; }  

inside the interface

It is internally hooked as get_SayHello( ) ,set_SayHello() in IL (when i disassemble i can see the get and set methods).

My question is still property needs some storage location,then how the property declaration

is allowed inside the interface.

Edit : This is what i understood.As I am new to C#,i am seeking your help.

like image 878
user160677 Avatar asked Sep 08 '09 15:09

user160677


1 Answers

You're operating on a somewhat faulty assumption, that properties require a backing field. Yes most properties use a backing field but this is certainly not a requirement. I can for instance implement your interface with no backing field as follows

class C1 : IFoo {
  public string SayHello {
    get { return "Say Hello"; }
    set { }
  }
}
like image 56
JaredPar Avatar answered Oct 15 '22 16:10

JaredPar