Can an Interface contain a variable?

93,977

Solution 1

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.

Solution 2

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);    
   }    
}

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
93,977
Sandeep
Author by

Sandeep

Updated on April 18, 2020

Comments