By 22/07/2015 14 Comments

Interface In C#

  • An Interface defines the term “what a class must do”. It enforces to the class to complete the uncompleted behavior (methods).
  • An object of the interface cannot be created.
  • In C# an interface can be a member of a class, namespace or struct.
  • An interface contains incomplete methods which is “abstract” methods by default.
  • An interface can only be used through its implementation in a class. When an interface is implemented in a class that class must complete its abstract/incomplete methods.
  • An interface can be declare by using a keyword “interface”.
  • Interfaces are similar to abstract classes but in an interface there are no complete method can be declared.
  • One interface can be implemented by any number of classes and also one class can implements any number of interfaces at once separated by commas. Along with interfaces implementation a class is free to declare its own members (methods, data etc.).
  • An interface can be inherited from other base/parent interfaces. Base interface member can be hidden in its derived interface by same declaration in it and generates a warning message. Warning message can be prevented through the use of “new” keyword with derived interface members.
  • An interface has “public” methods by default, no any access modifier can be explicitly specified with them.
  • Along with methods an interface has properties, indexers and events.
  • Interfaces cannot have fields/variables, destructors, constructors and operator methods.
  • Static members cannot be declared in interfaces.
  • The methods of interfaces must be specified as “public” in a class which implements it because these methods are implicitly public in an interface.
  • A class which implements an interface must has same method name, return type and parameter list exactly match with the methods declared in an interface.
  • C# implements runtime polymorphism through interfaces.
  • Interfaces support multiple inheritance.

General Syntax:

access-modifier interface IName
      // statement(s)

access-modifier It must be public (Recommended but an optional).
interface An interface keyword.
IName Can be any valid interface name which should be prefixed by I.
{ // statement(s); } That can be interface members (abstract methods, properties etc).


using System;
namespace csharpBasic
    // An interface declaration.
    public interface IMarkSheet
        // Double type abstract method declaration.
        double GetObtainMarks();
        double GetPercentage(double obtainMarks, double totalMarks);

        // An abstract property declaration.
        double GetTotalMarks

        double GetAspMarks();
        double GetJsMarks();

    // MarkSheet class implements an interface IMarkSheet.
    class MarkSheet : IMarkSheet
       // Double type fields declaration.
        double AspMarks;
        double JavascriptMarks;
        double TotalMarks = 200;

        // Double type read-only property declaration.
        public double GetTotalMarks
                return TotalMarks;

        // Parametrized constructor with 2 parameters.
        public MarkSheet(double aspMarks, double javascriptMarks)
            AspMarks = aspMarks;
            JavascriptMarks = javascriptMarks;

        // Double return type methods which returns each subject mark.
        public double GetAspMarks()
            return AspMarks;
        public double GetJsMarks()
            return JavascriptMarks;

        // Return type method returns obtain marks.
        public double GetObtainMarks()
            return AspMarks + JavascriptMarks;

        // Double return type method returns percentage of obtain marks.
        public double GetPercentage(double obtainMarks, double totalMarks)
            return (obtainMarks / totalMarks) * 100.0;

    class Program
        static void Main(string[] args)
            Console.WriteLine("General mark sheet information:");

            // Creation of a Marksheet object.
            MarkSheet markSheet = new MarkSheet(80, 70);

            Console.WriteLine("Asp.NET marks: {0}", markSheet.GetAspMarks());
            Console.WriteLine("Javascript marks: {0}", markSheet.GetJsMarks());

            // Statement gets obtain marks by calling GetObtainMarks() method.
            double obtainMarks = markSheet.GetObtainMarks();

            // Statement prints total marks by access TotalMarks static field.
            Console.WriteLine("Total Marks {0}", markSheet.GetTotalMarks);
            Console.WriteLine("Obtain marks: {0}", obtainMarks);
            Console.WriteLine("Pecentage: {0}", markSheet.GetPercentage(obtainMarks, markSheet.GetTotalMarks));
    /*  The Output will be:
        General mark sheet information:
        Asp.NET marks: 80
        Javascript marks: 70
        Total Marks 200
        Obtain marks: 150
        Pecentage: 75


Next Tutorial →

← Previous Tutorial

Posted in: C# Advance, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .