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

Wednesday, February 17, 2016

What is structure in c#?



In C sharp Structure are the light weight classes and also value type, it contains fields, methods, constants, constructor, indexer.
Some key points of structure are given below:
Structure does not inherited
Not protected modifier is used
We cannot create default constructor in structure
With new keyword and without new structure is used while in class new is necessary
Instance field initializer is not possible in the structure
The example of the structure is given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    struct mystruct
    {
       public  int x, y;
        public mystruct(int i, int j)
        {
            x = i;
            y = j;
        }
    }
}


Code on Button click:
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            mystruct obj = new mystruct();
            obj.x = 1;
            obj.y = 2;
            MessageBox.Show(obj.x.ToString());
            MessageBox.Show(obj.y.ToString());
           
        }
    }
}

Output :
1
2





Tuesday, February 16, 2016

Polymorphism in c # with example



Ability to take more than one form is called polymorphism. The polymorphism consist two words poly and morphism poly means “multiple” and morphism means “form”.
Following are the types of polymorphism

-Compile time polymorphism also called Early binding or overloading or static binding

-Run time polymorphism also called late binding or overriding or dynamic binding

Compile time polymorphism – In compile time polymorphism method name is same with different parameter (signature)
The example of the compile time polymorphism is given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace compile_time_poly
{
    public class Class1
    {
        public void show(int a, int b)
        {
            MessageBox.Show((a + b).ToString());
        }
        public void show(int a, int b, int c)
        {
            MessageBox.Show((a + b + c).ToString());
        }
    }
}

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 compile_time_poly
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            obj.show(5, 10);
            obj.show(5, 10, 15);
        }
    }
}


Output:
15
30
Run time polymorphism – In run time polymorphism method name and parameter should be same. Run time polymorphism is achieved by virtual and override keyword. The example of run time polymorphism is given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace compile_time_poly
{
    public class Class1
    {
        public virtual void show(int a, int b)
        {
            MessageBox.Show((a + b).ToString());
        }
        public class class2 : Class1
        {
            public override void show(int a, int b)
            {
                MessageBox.Show((a + b).ToString());
            }
        }
    }
}

Button click code :
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 compile_time_poly
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            obj.show(5, 10);
            obj.show(5, 15);
        }
    }
}


Output:
15
20