Structure is a user defined type which can contain different member (variable, method and property etc).
Class is a user define type which can contain such type of member but there is some difference between structure and class.
Now i am going to implement structure and class in C# after that i will show you exact difference between structure and class in C#.
There are some steps please follow them step by step which is given below.
Step 1 First open your visual studio->File->New->project->console Application->OK->Now write the following code which is given below in step 2.
Step 2 structure and class program code:-
see it:-
Step 3 Now Run the program(press F5).
See output:
Description:- Now i am going to explain how these above program will work.Please read each point to understand the whole concept of this program.
3. After that i have created a class employee.Class is a user define type which can contain such type of member but there is some difference between structure and class.
Now i am going to implement structure and class in C# after that i will show you exact difference between structure and class in C#.
There are some steps please follow them step by step which is given below.
Step 1 First open your visual studio->File->New->project->console Application->OK->Now write the following code which is given below in step 2.
Step 2 structure and class program code:-
see it:-
using System;
namespace structure
{
class Program
{
public struct student
{
public String sname;
public int sage;
public student(String s,int i)
{
sname=s;
sage=i;
Console.WriteLine("student name is"+" " + sname);
Console.WriteLine("student age is"+ " " + sage);
Console.ReadLine();
}
public void show()
{
Console.WriteLine("student name is" + " " + sname);
Console.WriteLine("student age is" + " " + sage);
Console.ReadLine();
}
public class employee
{
public String ename;
public int eage;
public employee()
{
ename="santosh";
eage =24;
}
public employee(String e, int j)
{
ename=e;
eage=j;
Console.WriteLine("employee name(parameter department) is"+" " + ename);
Console.WriteLine("employee age(parameter department) is" + " " + eage);
Console.ReadLine();
}
public void display()
{
Console.WriteLine("employee name is" + " " + ename);
Console.WriteLine("employee age is" + " " + eage);
Console.ReadLine();
}
}
static void Main(string[] args)
{ //calling the structure member
student st = new student("ram", 23);
st.sname = "vinod";
st.sage = 22;
st.show();
//calling the class member paramerized cosntructur
employee em = new employee("shayam", 33);
em.ename = "Rajesh";
em.eage = 21;
em.display();
//calling the class member empty costructur
employee em1 = new employee();
em1.display();
}
}
}
}
Step 3 Now Run the program(press F5).
See output:
Description:- Now i am going to explain how these above program will work.Please read each point to understand the whole concept of this program.
- First i have created a structure student with struct keyword.
- After that i have declared and defined following member of student structure which is give below.
- I have declared two variable sname and sage .
- I have created a constructor with two parameter.
- I have created a show() method for displaying the student member data.
4. After that i have declared and defined following member of employee class which is give below.
- I have declared two variable ename and eage .
- I have created a constructor with No parameter.
- I have created a constructor with two parameter.
- I have created a display() method for displaying the employee member data.
5. After that i have created object of student structure and passed two parameter(at the object creation) in the main method.
EX1.
student st=new student("ram",23);
Explanation:-
here parameterized constructor of student structure will call and copy the values in corresponding variables.
s<--ram
i<--23
sname<--ram
sage<--23
output:
student name is ram
student age is 23
EX2:Now again ,i have assigned values in variable of student sname and sage with the help of object st.
st.sname="vinod";
st.sage=22;
st.show();
here i have called the show() method of student to display the sname and sage values.
output:
student name is vinod
student age is 22
6. After that i have created the object of employee class and passed two parameter(at object creation ).
EX1:
employee em =new employee("shayam",33);
Explanation:-
here parameterized constructor of employee class will call and display the parameter values.
e<--shayam
j<--33
sname<--shyam
sage<--33
output:
employee name(parameter department) is shayam
employee age(parameter department) is 33
EX2:
Now again ,i have assigned values in variable of employee ename and eage with the help of object em.
em.ename="Rajesh";
em.eage=21;
st.display();
here i have called the display() method of employee class to display the ename and eage values.
output:
employee name is Rajesh
employee age is 21
7. After that i have called empty constructor of employee class by creating the object of employee class again for calling the empty constructor of the employee class.
employee em1 =new employee();
em1.display();
output:
employee name is Santosh
employee age is 24
Difference between Structure and class :-
- Structure is stored within stack memory and Class is stored within heap memory.
- In Structure we can not define parameter less constructor but in class can do it.
- In structure we can not initialize the data member at declaration but in class can do it.
- In structure inheritance is not possible(one structure can not inherited to another structure )but in class it is possible.
- We can not declare virtual override and abstract keyword with structure but in class it is possible.
- Class is Reference type but structure is value type.
- We can use Destructor in Class but not in Structure.
For More:-
please click below for download whole application.
nice post... do as well
ReplyDelete