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




Saturday, February 13, 2016

Maintaining Data Integrity in Sql

Data integrity tells about marinating the proper data. To maintain proper data we need to impose some business rules on data. The business rules are provide in the from at data integrity constraints which are going to restrict us,  If we don't maintain the integrity.
  1. Not null
  2. Unique
  3. Primary Key
  4. Default
  5. Check
  6. Foreign Key
1 -  Not Null

If this constraint is imposed on a column it will not allow null values on it. We can impose 'n' number of not null constraints on a table.

Syntax

Create Table <Table Name> (Col1 <Data Type>[Width][Not Nulll],Col2 <Data Type>[Width][Not Nulll],.........................Coln <Data Type>[Width][Not Nulll]

Example

Create Table Employee(empid int Not Null, ename varchar(50), salary decimal(7,2)  Not Null

If you try to insert values in Employee table like

Insert into Employee(101,'Sharad',NULL) ->ERROR

Note

The draw of Not Null constraints is even if it restricts null values. It will also allow duplicate values.

2 -  Unique

If this constraint is impose on a column, It will not allow the duplicate values.

Example

Create Table Employee(empid int Unique, ename varchar(50), salary decimal(7,2)  Not Null

Note
  • The not null constraint is a part of the table, which  stores along with column in the table and when constraint is violated it will display us the name of the constraint on which the violation has been done.
  • The rest of constraints are separated object in the database apart from table and linked up the column because they are separated jobs they have their own name for identification. So if these constraints are violated they display their own identity but not the name of column.
  • So when this constraint are violated using the name only we need to identity where the violation has been done. Now follow the following convention and provide name to constraints.
Example

Create Table Employee(empid int constraint empid Unique, ename varchar(50), salary decimal(7,2)  Not Null

A constraint can be defined or apply in 2 ways
  1. Column level definition
  2. Table level definition
1 - Column level definition

In this case the details of constraints will provide immediately after column details as if in our previous example.

2 - Table level definition

In this case first all the columns are define, and in the end we define the constraint of the column.

Example

Create Table Employee(empid int , ename varchar(50), salary decimal(7,2)  Not Null, constraint empid_uq Unquie(empid))

Note

There is no different in behavior however we define the constraint either in table level or column level but the advantage is it, A constraint is defined at table level we can go for a composite constraint that is single on multiple columns. Basically the composite unique constraint checks the uniqueness the combination of columns but not single column.

Example

Create Table Employee_Deatils(city_code varchar(10),area_code varchar(10), address varchar(40), constraint cc_ac_uq  unique (city_code,area_code))

Note

The drawback of the unique constraint is it allow null value on it.

3 - Primary Key

This constraint is a combination of unique and not null, which restricts duplicate values as well as null values also. A table can have only one primary key constraint present on it, which can be either single column, or multiple columns.

Example

Create Table Employee(empid int constraint eid primary key, ename varchar(50), salary decimal(7,2)  Not Null

Composite Primary Key Example

Create Table Employee_Deatils(city_code varchar(10),area_code varchar(10), address varchar(40), constraint cc_ac_pkprimary key(city_code,area_code))

4 - Default Constraint

The default value of any column in a table is null unless a not null constraint or primary key imposed on it. We can change the default value of a column according to our need by using default constraint. The value which is specified as a default value comes into the picture when no value is specified to the column while inserting data into table.

Example

Create Table Employee(empid int , ename varchar(50), salary decimal(7,2)  default 2000)

When you insert a row into table and does not provide salary column value it automatically insert 2000 in salary column.

5 - Check Constraint

This is used for checking the value within a common to be according specification as following
  • Check (salary>=2000)
  • Check (salary >= 2000 And salary<=5000)
  • Check (salary ==2000 Or salary=5000)
Example

Create Table Employee(empid int , ename varchar(50), salary decimal(7,2)  Not Null constraint sal_ck Check(salary>=30000))

6 - Referential Integrity Constraint

This constraint is used for establishing master child relation between tables. To establish the relationship we require the following.
  • Master Table  :-  Which is going to contain the initial information in it.
  • Child Table : - Which should contain the detail information in it.
  • A reference key column in the master table which provide the reference values for the child table. This provides the reference values for the child table. This reference key column should contain either a primary key constraint or a unique constraint on it.
  • A foreign key column on a child table, which should restrict the values into the column, that are not present in the reference key column for doing this it has been imposed with a foreign key constraint on it.