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 *

Comments

austin faith

Avery good write-up. Please let me know what are the types of C# libraries used for AI development.

kariya arti

very satisfied!!

jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

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