WHAT IS INTERFACE IN C#

  • Explain interface

Interface in C# is a blueprint of a class. It is like abstract class because all the methods which are declared inside the interface are abstract methods. It cannot have method body and cannot be instantiated.
It is used to achieve multiple inheritance which can't be achieved by class. It is used to achieve fully abstraction because it cannot have method body.
Its implementation must be provided by class or struct. The class or struct which implements the interface, must provide the implementation of all the methods declared inside the interface.


  • Example

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
interface IStudent{    int subtotal(int s1, int s2, int s3, int s4);    float per(int total);    string grade(float per);     

}class Customer : IStudent{       public  int  subtotal(int s1,int s2,int s3,int s4)    {                       int total = s1+s2+s3+s4;        return total;    }    public float per(int total)    {        int per = total / 4;        return per;    }    public string grade(float per)    {        float pers = per;        if (pers < 50)        {            return "D";        }        else if (pers < 60)        {            return "C";        }        else if (pers < 70)        {            return "B";        }        else        {            return "A";        }    }
}public class program{    public static void Main()    {        Customer cs = new Customer();        int m1 = 50;        int m2 = 60;        int m3 = 70;        int m4 = 80;               Console.WriteLine("Total  :"+ cs.subtotal(m1, m2, m3, m4));        int total = cs.subtotal(m1, m2, m3, m4);        float persent= cs.per(total);        Console.WriteLine("persentage  :"+persent);       string grade = cs.grade(persent);        Console.WriteLine("Grade  :"+grade);        Console.Read();

    }    }

Comments

Popular posts from this blog

Search Record From Table Using jQuery in Asp.net core MVC

How To insert Radio Button Data in DataBase -- MVC

Toolbox Webparts Controls