top

Search

C# Tutorial

Interfaces in C# contain the definitions for different functionalities that classes can inherit. The interface members are only declared in the interface and they need to be defined in the class that derives them.Interfaces are quite useful when a class needs to inherit functionalities from multiple sources as multiple inheritance is not allowed in C#.Interface DefinitionThe keyword interface is used for interface definition. The interface names begin with a capital I because of convention.The syntax for an interface definition is given as follows:interface Iinterface_name {      // interface body }In the above syntax, interface is the keyword and the Iinterface_name is the name of the interface that begins with I.A program that demonstrates interface implementation is given as follows:Source Code: Program that demonstrates interface implementation in C#using System; namespace InterfaceDemo {    public interface IStudent    {      void putData();    }    public class Student: IStudent    {        private int rno;        private string name;        private double marks;        public Student(int r, string n, double m)        {            rno = r;            name = n;            marks = m;        }        public void putData()        {            Console.WriteLine("Roll Number = {0}", rno);            Console.WriteLine("Name = {0}", name);            Console.WriteLine("Marks = {0}", marks);            Console.WriteLine();        }    }    class Test    {      static void Main(string[] args)      {         Student s1 = new Student(1, "John", 78.5);         Student s2 = new Student(2, "Sara", 60.0);         Student s3 = new Student(3, "Bruce", 98.5);         s1.putData();         s2.putData();         s3.putData();      }    } }The output of the above program is as follows:Roll Number = 1 Name = John Marks = 78.5 Roll Number = 2 Name = Sara Marks = 60 Roll Number = 3 Name = Bruce Marks = 98.5Now let us understand the above program.The interface IStudent contains the declaration for the method putData(). The code snippet for this is as follows:public interface IStudent    {      void putData();    }The class Student inherits the interface IStudent. It contains the private data variables rno, name and marks. It contains a parameterized constructor that initializes the values of rno, name and marks. Also the definition for putData() is provided in class Student. The values of rno, name and marks are displayed. The code snippet for this is as follows:public class Student: IStudent    {        private int rno;        private string name;        private double marks;        public Student(int r, string n, double m)        {            rno = r;            name = n;            marks = m;        }        public void putData()        {            Console.WriteLine("Roll Number = {0}", rno);            Console.WriteLine("Name = {0}", name);            Console.WriteLine("Marks = {0}", marks);            Console.WriteLine();        }    }The function main() contains the objects s1, s2 and s3 of class Student. Then the function putData() is called for s1, s2 and s3. The code snippet for this is as follows: static void Main(string[] args)      {         Student s1 = new Student(1, "John", 78.5);         Student s2 = new Student(2, "Sara", 60.0);         Student s3 = new Student(3, "Bruce", 98.5);         s1.putData();         s2.putData();         s3.putData();      }
logo

C# Tutorial

Interfaces in C#

Interfaces in C# contain the definitions for different functionalities that classes can inherit. The interface members are only declared in the interface and they need to be defined in the class that derives them.

Interfaces are quite useful when a class needs to inherit functionalities from multiple sources as multiple inheritance is not allowed in C#.

Interface Definition

The keyword interface is used for interface definition. The interface names begin with a capital I because of convention.

The syntax for an interface definition is given as follows:

interface Iinterface_name
{
     // interface body
}

In the above syntax, interface is the keyword and the Iinterface_name is the name of the interface that begins with I.

A program that demonstrates interface implementation is given as follows:

Source Code: Program that demonstrates interface implementation in C#

using System;
namespace InterfaceDemo
{
   public interface IStudent
   {
     void putData();
   }
   public class Student: IStudent
   {
       private int rno;
       private string name;
       private double marks;
       public Student(int r, string n, double m)
       {
           rno = r;
           name = n;
           marks = m;
       }
       public void putData()
       {
           Console.WriteLine("Roll Number = {0}", rno);
           Console.WriteLine("Name = {0}", name);
           Console.WriteLine("Marks = {0}", marks);
           Console.WriteLine();
       }
   }
   class Test
   {
     static void Main(string[] args)
     {
        Student s1 = new Student(1, "John", 78.5);
        Student s2 = new Student(2, "Sara", 60.0);
        Student s3 = new Student(3, "Bruce", 98.5);
        s1.putData();
        s2.putData();
        s3.putData();
     }
   }
}

The output of the above program is as follows:

Roll Number = 1
Name = John
Marks = 78.5

Roll Number = 2
Name = Sara
Marks = 60

Roll Number = 3
Name = Bruce
Marks = 98.5

Now let us understand the above program.

The interface IStudent contains the declaration for the method putData(). The code snippet for this is as follows:

public interface IStudent
   {
     void putData();
   }

The class Student inherits the interface IStudent. It contains the private data variables rno, name and marks. It contains a parameterized constructor that initializes the values of rno, name and marks. Also the definition for putData() is provided in class Student. The values of rno, name and marks are displayed. The code snippet for this is as follows:

public class Student: IStudent
   {
       private int rno;
       private string name;
       private double marks;
       public Student(int r, string n, double m)
       {
           rno = r;
           name = n;
           marks = m;
       }
       public void putData()
       {
           Console.WriteLine("Roll Number = {0}", rno);
           Console.WriteLine("Name = {0}", name);
           Console.WriteLine("Marks = {0}", marks);
           Console.WriteLine();
       }
   }

The function main() contains the objects s1, s2 and s3 of class Student. Then the function putData() is called for s1, s2 and s3. The code snippet for this is as follows:

 static void Main(string[] args)
     {
        Student s1 = new Student(1, "John", 78.5);
        Student s2 = new Student(2, "Sara", 60.0);
        Student s3 = new Student(3, "Bruce", 98.5);
        s1.putData();
        s2.putData();
        s3.putData();
     }

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial