Interface in C#

By
There are some good features of Interface in C#. Which are given below.
  • Interface is the type which contain only declare member.It can not even single define member.
  • Interface can support multiple inheritance.
  • We can not specify any access specifier with interface member.
  •   In c#Interface can contains property definitions also.
  • We can not use Abstract keyword with Interface.
  • We can can not implement methods in interface.
  • We can not use constructor and destructor  with interface.
Example of Interface in c#:
Now i am going to implement the Interface concept in c#.please follow these steps one by one. 
Step 1 First open your visual studio->File->New ->project->Console Application->put any Name(Interface)-> OK->write the c# code which is given in step 2.
Step 2 Interface C# program code:-
using System;
namespace @interface
{
    class Program
    {
        public interface employee
        {
            void show(String s);
            String propName
            {
                get;
                set;
            }

        }
        public class student : employee
        {
            String sname;
            String[] marks = new String[5];
            public void show(String s)
            {
                Console.Write("hello"+" " + s);
                Console.Write("\n");
            }
            public void add(int i, int j)
            {
                int sum=i+j;
                Console.WriteLine(sum);
                
            }
            public String propName
            {
                get
                {
                    return sname;
                }
                set
                {
                    sname = value;
                }
            }
        }
        static void Main(string[] args)
        {
            student obj = new student();
            obj.propName = "INDIA";
            Console.WriteLine(obj.propName);
            obj.show("CUSTOMER");
            obj.add(40,50);
            Console.ReadLine();
        }  
    }
}

Step 3  Now Run the Program(press F5).
See Output:-

Description:- 
There are some description of Interface in c#.please read carefully to understand the whole concept of Interface.
  • First ,i have created an Interface  employee.
  • After that i have declared a show method with one parameter type string without any definition.
  • After that i have created property with get and set access-or without any definition.
  • Here with  the interface member i have not used any access specifier.
  • After that i have created a student class(child class) which Inherit the employee interface.
  • Within the student class ,i have created marks array of size 5.
  • In child class(student),i have given the definition of show and property method which is already mentioned in employee interface.
  • In student class ,i have taken a separate method add  for adding two values which is not mentioned in interface employee.
  • After that i have created the object of student class(child class),so that we can call and assign the value in interface  employee member as well as student member
  • colsole.ReadLine() is used for stopping the console application in c# like getch() in c.
Note:-Interface came in C# for supporting the multiple inheritance because c#,c++,java was not supported the multiple inheritance. 
For More:-

  1.  oops concepts
  2. Basic elements for compiling the code
  3. Generate unique Number and save it in the database
  4. Take Print receipt in Windows Form Application
  5.   Xml Application
I hope this is helpful for you.
To Get the Latest  Free Updates Subscribe
please click below for download the whole application
   Download

2 comments:

Powered by Blogger.