Properties can be declared on an interface. The following example declares an interface property accessor:
sample · csharpdocs/csharp/programming-guide/classes-and-structs/snippets/properties/interfaces.csSnippetDeclareInterfaceProperties
using System;
namespace interfaces
{
// <SnippetDeclareInterfaceProperties>
public interface ISampleInterface
{
// Property declaration:
string Name
{
get;
set;
}
}
// </SnippetDeclareInterfaceProperties>
interface ICitizen
{
string Name
{
get;
set;
}
}
public class Employee2 : IEmployee, ICitizen
{
//<SnippetExplicitImplementation>
string IEmployee.Name
{
get { return "Employee Name"; }
set { }
}
//</SnippetExplicitImplementation>
//<SnippetCitizenImplementation>
string ICitizen.Name
{
get { return "Citizen Name"; }
set { }
}
//</SnippetCitizenImplementation>
public int Counter => 0;
}
//<SnippetPropertyExample>
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 => _name;
set => _name = value;
}
private int _counter;
public int Counter // read-only instance property
{
get => _counter;
}
// constructor
public Employee() => _counter = ++numberOfEmployees;
}
//</SnippetPropertyExample>
public static class Properties
{
public static void Examples()
{
// <SnippetUseProperty>
System.Console.Write("Enter number of employees: ");
Employee.numberOfEmployees = int.Parse(System.Console.ReadLine());
Employee e1 = new Employee();
System.Console.Write("Enter the name of the new employee: ");
e1.Name = System.Console.ReadLine();
System.Console.WriteLine("The employee information:");
System.Console.WriteLine($"Employee number: {e1.Counter}");
System.Console.WriteLine($"Employee name: {e1.Name}");
// </SnippetUseProperty>
}
}
}Interface properties typically don't have a body. The accessors indicate whether the property is read-write, read-only, or write-only. Unlike in classes and structs, declaring the accessors without a body doesn't declare an automatically implemented property. An interface can define a default implementation for members, including properties. Defining a default implementation for a property in an interface is rare because interfaces can't define instance data fields.
Example
In this example, the interface IEmployee has a read-write property, Name, and a read-only property, Counter. The class Employee implements the IEmployee interface and uses these two properties. The program reads the name of a new employee and the current number of employees and displays the employee name and the computed employee number.
You could use the fully qualified name of the property, which references the interface in which the member is declared. For example:
sample · csharpdocs/csharp/programming-guide/classes-and-structs/snippets/properties/interfaces.csSnippetExplicitImplementation
using System;
namespace interfaces
{
// <SnippetDeclareInterfaceProperties>
public interface ISampleInterface
{
// Property declaration:
string Name
{
get;
set;
}
}
// </SnippetDeclareInterfaceProperties>
interface ICitizen
{
string Name
{
get;
set;
}
}
public class Employee2 : IEmployee, ICitizen
{
//<SnippetExplicitImplementation>
string IEmployee.Name
{
get { return "Employee Name"; }
set { }
}
//</SnippetExplicitImplementation>
//<SnippetCitizenImplementation>
string ICitizen.Name
{
get { return "Citizen Name"; }
set { }
}
//</SnippetCitizenImplementation>
public int Counter => 0;
}
//<SnippetPropertyExample>
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 => _name;
set => _name = value;
}
private int _counter;
public int Counter // read-only instance property
{
get => _counter;
}
// constructor
public Employee() => _counter = ++numberOfEmployees;
}
//</SnippetPropertyExample>
public static class Properties
{
public static void Examples()
{
// <SnippetUseProperty>
System.Console.Write("Enter number of employees: ");
Employee.numberOfEmployees = int.Parse(System.Console.ReadLine());
Employee e1 = new Employee();
System.Console.Write("Enter the name of the new employee: ");
e1.Name = System.Console.ReadLine();
System.Console.WriteLine("The employee information:");
System.Console.WriteLine($"Employee number: {e1.Counter}");
System.Console.WriteLine($"Employee name: {e1.Name}");
// </SnippetUseProperty>
}
}
}The preceding example demonstrates Explicit Interface Implementation. For example, if the class Employee is implementing two interfaces ICitizen and IEmployee and both interfaces have the Name property, the explicit interface member implementation is necessary. That is, the following property declaration:
sample · csharpdocs/csharp/programming-guide/classes-and-structs/snippets/properties/interfaces.csSnippetExplicitImplementation
using System;
namespace interfaces
{
// <SnippetDeclareInterfaceProperties>
public interface ISampleInterface
{
// Property declaration:
string Name
{
get;
set;
}
}
// </SnippetDeclareInterfaceProperties>
interface ICitizen
{
string Name
{
get;
set;
}
}
public class Employee2 : IEmployee, ICitizen
{
//<SnippetExplicitImplementation>
string IEmployee.Name
{
get { return "Employee Name"; }
set { }
}
//</SnippetExplicitImplementation>
//<SnippetCitizenImplementation>
string ICitizen.Name
{
get { return "Citizen Name"; }
set { }
}
//</SnippetCitizenImplementation>
public int Counter => 0;
}
//<SnippetPropertyExample>
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 => _name;
set => _name = value;
}
private int _counter;
public int Counter // read-only instance property
{
get => _counter;
}
// constructor
public Employee() => _counter = ++numberOfEmployees;
}
//</SnippetPropertyExample>
public static class Properties
{
public static void Examples()
{
// <SnippetUseProperty>
System.Console.Write("Enter number of employees: ");
Employee.numberOfEmployees = int.Parse(System.Console.ReadLine());
Employee e1 = new Employee();
System.Console.Write("Enter the name of the new employee: ");
e1.Name = System.Console.ReadLine();
System.Console.WriteLine("The employee information:");
System.Console.WriteLine($"Employee number: {e1.Counter}");
System.Console.WriteLine($"Employee name: {e1.Name}");
// </SnippetUseProperty>
}
}
}Implements the Name property on the IEmployee interface, while the following declaration:
sample · csharpdocs/csharp/programming-guide/classes-and-structs/snippets/properties/interfaces.csSnippetCitizenImplementation
using System;
namespace interfaces
{
// <SnippetDeclareInterfaceProperties>
public interface ISampleInterface
{
// Property declaration:
string Name
{
get;
set;
}
}
// </SnippetDeclareInterfaceProperties>
interface ICitizen
{
string Name
{
get;
set;
}
}
public class Employee2 : IEmployee, ICitizen
{
//<SnippetExplicitImplementation>
string IEmployee.Name
{
get { return "Employee Name"; }
set { }
}
//</SnippetExplicitImplementation>
//<SnippetCitizenImplementation>
string ICitizen.Name
{
get { return "Citizen Name"; }
set { }
}
//</SnippetCitizenImplementation>
public int Counter => 0;
}
//<SnippetPropertyExample>
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 => _name;
set => _name = value;
}
private int _counter;
public int Counter // read-only instance property
{
get => _counter;
}
// constructor
public Employee() => _counter = ++numberOfEmployees;
}
//</SnippetPropertyExample>
public static class Properties
{
public static void Examples()
{
// <SnippetUseProperty>
System.Console.Write("Enter number of employees: ");
Employee.numberOfEmployees = int.Parse(System.Console.ReadLine());
Employee e1 = new Employee();
System.Console.Write("Enter the name of the new employee: ");
e1.Name = System.Console.ReadLine();
System.Console.WriteLine("The employee information:");
System.Console.WriteLine($"Employee number: {e1.Counter}");
System.Console.WriteLine($"Employee name: {e1.Name}");
// </SnippetUseProperty>
}
}
}Implements the Name property on the ICitizen interface.
sample · csharpdocs/csharp/programming-guide/classes-and-structs/snippets/properties/interfaces.csSnippetPropertyExample
using System;
namespace interfaces
{
// <SnippetDeclareInterfaceProperties>
public interface ISampleInterface
{
// Property declaration:
string Name
{
get;
set;
}
}
// </SnippetDeclareInterfaceProperties>
interface ICitizen
{
string Name
{
get;
set;
}
}
public class Employee2 : IEmployee, ICitizen
{
//<SnippetExplicitImplementation>
string IEmployee.Name
{
get { return "Employee Name"; }
set { }
}
//</SnippetExplicitImplementation>
//<SnippetCitizenImplementation>
string ICitizen.Name
{
get { return "Citizen Name"; }
set { }
}
//</SnippetCitizenImplementation>
public int Counter => 0;
}
//<SnippetPropertyExample>
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 => _name;
set => _name = value;
}
private int _counter;
public int Counter // read-only instance property
{
get => _counter;
}
// constructor
public Employee() => _counter = ++numberOfEmployees;
}
//</SnippetPropertyExample>
public static class Properties
{
public static void Examples()
{
// <SnippetUseProperty>
System.Console.Write("Enter number of employees: ");
Employee.numberOfEmployees = int.Parse(System.Console.ReadLine());
Employee e1 = new Employee();
System.Console.Write("Enter the name of the new employee: ");
e1.Name = System.Console.ReadLine();
System.Console.WriteLine("The employee information:");
System.Console.WriteLine($"Employee number: {e1.Counter}");
System.Console.WriteLine($"Employee name: {e1.Name}");
// </SnippetUseProperty>
}
}
}sample · csharpdocs/csharp/programming-guide/classes-and-structs/snippets/properties/interfaces.csSnippetUseProperty
using System;
namespace interfaces
{
// <SnippetDeclareInterfaceProperties>
public interface ISampleInterface
{
// Property declaration:
string Name
{
get;
set;
}
}
// </SnippetDeclareInterfaceProperties>
interface ICitizen
{
string Name
{
get;
set;
}
}
public class Employee2 : IEmployee, ICitizen
{
//<SnippetExplicitImplementation>
string IEmployee.Name
{
get { return "Employee Name"; }
set { }
}
//</SnippetExplicitImplementation>
//<SnippetCitizenImplementation>
string ICitizen.Name
{
get { return "Citizen Name"; }
set { }
}
//</SnippetCitizenImplementation>
public int Counter => 0;
}
//<SnippetPropertyExample>
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 => _name;
set => _name = value;
}
private int _counter;
public int Counter // read-only instance property
{
get => _counter;
}
// constructor
public Employee() => _counter = ++numberOfEmployees;
}
//</SnippetPropertyExample>
public static class Properties
{
public static void Examples()
{
// <SnippetUseProperty>
System.Console.Write("Enter number of employees: ");
Employee.numberOfEmployees = int.Parse(System.Console.ReadLine());
Employee e1 = new Employee();
System.Console.Write("Enter the name of the new employee: ");
e1.Name = System.Console.ReadLine();
System.Console.WriteLine("The employee information:");
System.Console.WriteLine($"Employee number: {e1.Counter}");
System.Console.WriteLine($"Employee name: {e1.Name}");
// </SnippetUseProperty>
}
}
}Sample output
Enter number of employees: 210
Enter the name of the new employee: Hazem Abolrous
The employee information:
Employee number: 211
Employee name: Hazem Abolrous