An interface contains only the signatures of properties and methods.
The implementation of the methods is done in the class that implements the interface.Interfaces can contain:
- methods
- properties
- events
- indexers
An interface can't contain:
- constants
- fields
- operators
- instance constructors
- destructors
- types
Example:
using System;
using System.Collections.Generic;using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplicationCS
{interface IAnimal
{
string Name { get; set; }
string Type { get; set; }
}
class Dog : IAnimal, IComparable
{
private string strName;
private string strType;
public Dog(string name)
{
this.Name = name;
}
public string Name
{
get { return strName; }
set { strName = value; }
}
public string Type
{
get { return strType; }
set { strType = value; }
}
public int CompareTo(object obj)
{
if (obj is IAnimal)
return this.Name.CompareTo((obj as IAnimal).Name);
return 0;
}
}
public partial class Form1 : Form
{public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{Dog dog1 = new Dog("Dog1");
dog1.Type = "Carnivore";
Dog dog2 = new Dog("Dog2");
dog2.Type = "Carnivore";
Dog dog3 = new Dog("Dog3");
dog3.Type = "Carnivore";
int x = dog1.CompareTo(dog2); // reurn -1
int y = dog1.CompareTo(dog3); // reurn 0}
}
}