46

Possible Duplicate:
Why can't C# interfaces contain fields?

Hi all,

Jon Skeet has answered to a question that Using a property is backed by a variable.

But properties in Interfaces are allowed in C#. Does that mean the interfaces in C# can contain a variable and how would the variable backed by the property will be handled?

Thanks in advance.

Community
  • 1
  • 1
Sandeep
  • 7,156
  • 12
  • 45
  • 57
  • @Gishu - (+1) thanks for pointing to the right thread. ------------------------------------------------------ @Sandeep - In simple terms, an interface can contain method declarations only. Properties, events or indexers are variants of methods. Getter & setter in Properties are just variants of methods. Please refer "CLR via C#" OR "Essential .net Volume 1" for more details. – Sandeep G B Apr 18 '11 at 05:42

5 Answers5

34

An interface can be a member of a namespace or a class and can contain signatures of the following members:

Methods

Properties

Indexers

Events

Properties can be declared on interfaces. The declaration takes the following form: The accessor of an interface property does not have a body.

Thus, the purpose of the accessors is to indicate whether the property is read-write, read-only, or write-only.

Example:

// Interface Properties    
interface IEmployee
{
   string Name
   {
      get;
      set;
   }

   int Counter
   {
      get;
   }
}

Implementation:

public class Employee: IEmployee 
{
   public static int numberOfEmployees;

   private int counter;

   private string name;

   // Read-write instance property:
   public string Name
   {
      get
      {
         return name;
      }
      set
      {
         name = value;
      }
   }

   // Read-only instance property:
   public int Counter
   {    
      get    
      {    
         return counter;
      }    
   }

   // Constructor:
   public Employee()
   {
      counter = ++counter + numberOfEmployees;
   }
}  

MainClass:

public class MainClass
{
   public static void Main()
   {    
      Console.Write("Enter number of employees: ");

      string s = Console.ReadLine();

      Employee.numberOfEmployees = int.Parse(s);

      Employee e1 = new Employee();

      Console.Write("Enter the name of the new employee: ");

      e1.Name = Console.ReadLine();  

      Console.WriteLine("The employee information:");

      Console.WriteLine("Employee number: {0}", e1.Counter);

      Console.WriteLine("Employee name: {0}", e1.Name);    
   }    
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Abhi
  • 5,501
  • 17
  • 78
  • 133
32

No. An interface cannot contain a field.

An interface can declare a Property, but it doesn't provide any implementation of it, so there's no backing field. It's only when a class implements an interface that a backing field (or automatic property) is needed.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • Thanks for answering. Though the property is syntactic sugar for get set method of a field, when used in Interface only get set methods are present excluding the field. Please correct me If I understood it wrong. – Sandeep Apr 18 '11 at 13:00
  • 3
    @Sandeep - A field doesn't have get/set methods. A field is just an item of storage associated with a class. A property is a member that is accessed in client code like a field, but allows execution of code to determine the value to be returned, or to do some processing when a value is set. A property may be associated with one or more fields, but doesn't have to be. An automatic property (`int Property { get; set; }`) is syntactic sugar for a property that just gets and sets a private backing field. – Andrew Cooper Apr 18 '11 at 17:29
  • 1
    @Sandeep - An interface only specifies what client code should be able to do with an instance of a class that implements the interface. It doesn't specify anything about how the class handle the interactions. Therefore it just includes declarations for properties, method, events and indexers, with no implementation. A Property declaration in a interface looks the same as an automatic property in a class, but there is no backing field and it doesn't actually do anything unless a class implements the interface. – Andrew Cooper Apr 18 '11 at 17:35
5

Properties can be declared on an interface (not defined). But the accessor of an interface property does not have a body. Thus, the purpose of the accessors is to indicate whether the property is read-write, read-only, or write-only.

If you check out MSDN Doc on this then you'll see that you don't define property in an interface instead the implementation is done by the inheriting Class.

interface IEmployee
{
    string Name
    {
        get;
        set;
    }

    int Counter
    {
        get;
    }
}

public class Employee : IEmployee
{
    public static int numberOfEmployees;

    private string name;
    public string Name  // read-write instance property
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    private int counter;
    public int Counter  // read-only instance property
    {
        get
        {
            return counter;
        }
    }

    public Employee()  // constructor
    {
        counter = ++counter + numberOfEmployees;
    }
}

So in the interface IEmployee the Syntax of properties looks like auto-implemented properties but they are just Indicating whether the property is read-write, read-only, or write-only, nothing more. Its the task of implementing class to do the Implementation and define the properties.

Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
2

No, it doesn't mean that. The interface doesn't actually contain the property, either.

Remember than interface has to be implemented by a class in order for you to use it. An interface only defines a contract, meaning that any class that implements that interface will have all of the members defined by the interface. The interface doesn't actually have those members; it's more like a template. The instance of the class that implements the interface is the one that contains the property, and therefore the private backing field used for the property.

Interfaces can only define signatures for the following members:

  • Methods
  • Properties
  • Indexers
  • Events
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
2

No, the interface is only the "contract". It's up to the implementing class implement the property; it may or may not use a variable to back it.

Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
  • I think you're getting confused because the syntax of "auto-backed" properties is the same syntax of interface properties. In a class however, the compiler inserts the backing variable for you; ie. an auto-backed property is just syntactic sugar. – Esteban Araya Apr 18 '11 at 04:28