Saturday, February 20, 2016

What is interface in C# with example?



Interface is like a class but has no implementation it contains only declaration methods, properties and indexer .It is declared by the interface keywords in place of class.

The example of the interface is given below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    interface A
    {
        void show1();
    }
    interface B
    {
        void show2();
    }
    public class classC:A,B
    {
        public void show1()
        {
            MessageBox.Show("hi");
        }
        public void show2()
        {
            MessageBox.Show("hello");
        }
    }
}

Code on button click:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            classC obj = new classC();
            obj.show1();
            obj.show2();
        }
    }
}
 
Output:
hi
hello

No comments:

Post a Comment