Defining an interface

  1. Which members are possible to define in an interface?
  2. How to define an interface?
  3. Implementation of inheriting interfaces

The C# allows to define such members as methods, events, parameterless properties and parameterful properties(indexers in C#). However it is not possible to define constructors and fields.

The CLR allows defining constants, static fields, static methods and static constructs, but not all compilers support them. C# doesn’t support all the members above.

Examples:

using System;

 

namespace Interfaces

{

    public delegate void MyEventHandler(int param);

 

    internal interface IBaseInterface

    {

        void Method();

        int Property { set; get; }

        int this[int index] { set; get; }

        event MyEventHandler MyEvent;

    }

 

    internal interface IDerivedInterface : IBaseInterface

    {

//’Interfaces.IDerivedInterface.Method()’ hides inherited member //’Interfaces.IBaseInterface.Method()’.

//Use the new keyword if hiding was intended.

new void Method();

    }

 

    public class BaseInterfaceClass : IBaseInterface

    {

        public void Method() { Console.WriteLine("Method of base interface"); }

 

        private int _propertyField = 0;

 

        public int Property

        {

            set { _propertyField = value; }

            get { return _propertyField; }

        }

 

        private int _indexerField = 0;

 

        public int this[int index]

        {

            set { _indexerField = index; }

            get { return _indexerField; }

        }

 

        public event MyEventHandler MyEvent;

    }

 

    public class DerivedInterfaceClass : IDerivedInterface

    {

        public void Method() { Console.WriteLine("Method of derived interface"); }

 

        private int _propertyField = 0;

 

        public int Property

        {

            set { _propertyField = value; }

            get { return _propertyField; }

        }

 

        private int _indexerField = 0;

 

        public int this[int index]

        {

            set { _indexerField = index; }

            get { return _indexerField; }

        }

 

        public event MyEventHandler MyEvent;

    }

}

 

As for types we can’t specify visibility/accessibility which is different from public or internal in a namespaces scope and the C# allows specifying visibility/accessibility inside of a type scope.

By conventional all interfaces must have an upper-case “I” prefix.

There are two simple rules about an interface inheritance:

1. Any class which implements an interface which is inherited from other interfaces must implement all members of all the base interfaces.

2. Any code that uses a class which implements an interface and is inherited from other interfaces can expect of implementation of all the base interfaces.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s