C# Interface – Programs for Practice


Q)What is output of below C# code?

interface IStudent
{   
    string Name
    {
        get;
        set;
    }
    void study();
}

public class Student : IStudent
{   
    private string name;

    public string Name 
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }
   public void study()
    {
        Console.WriteLine("Study");                
    }
}

class Program
{
    static void Main()
    {
        Student s = new Student();
        Console.Write("Enter Student name: ");
        s.Name = System.Console.ReadLine();        
        Console.WriteLine("Student name: {0}", s.Name);
        s.study();       
    }
}

Output: What ever name we enter on cosole, that will be printed and then Study will be printed.
Explanation: In this C# program example, the class Student is implementing an interface called IStudent that contains a properties called Name and a method study().
Note that an interface can contain properties and method both.


Related Posts