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 betweenstructure 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 betweenstructure 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:-
01 | using System; |
02 | namespace structure |
03 | { |
04 | class Program |
05 | { |
06 | public struct student |
07 | { |
08 | public String sname; |
09 | public int sage; |
10 | public student(String s,int i) |
11 | { |
12 | sname=s; |
13 | sage=i; |
14 | Console.WriteLine("student name is"+" " + sname); |
15 | Console.WriteLine("student age is"+ " " + sage); |
16 | Console.ReadLine(); |
17 | |
18 | } |
19 | public void show() |
20 | { |
21 | Console.WriteLine("student name is" + " " + sname); |
22 | Console.WriteLine("student age is" + " " + sage); |
23 | Console.ReadLine(); |
24 | } |
25 | public class employee |
26 | { |
27 | public String ename; |
28 | public int eage; |
29 |
30 | public employee() |
31 | { |
32 | ename="santosh"; |
33 | eage =24; |
34 | } |
35 | public employee(String e, int j) |
36 | { |
37 | ename=e; |
38 | eage=j; |
39 | Console.WriteLine("employee name(parameter department) is"+" " + ename); |
40 | Console.WriteLine("employee age(parameter department) is" + " " + eage); |
41 | Console.ReadLine(); |
42 | } |
43 | public void display() |
44 | { |
45 | Console.WriteLine("employee name is" + " " + ename); |
46 | Console.WriteLine("employee age is" + " " + eage); |
47 | Console.ReadLine(); |
48 | } |
49 | } |
50 | static void Main(string[] args) |
51 | { //calling the structure member |
52 | student st = new student("ram", 23); |
53 | st.sname = "vinod"; |
54 | st.sage = 22; |
55 | st.show(); |
56 |
57 | //calling the class member paramerized cosntructur |
58 | employee em = new employee("shayam", 33); |
59 | em.ename = "Rajesh"; |
60 | em.eage = 21; |
61 | em.display(); |
62 |
63 | //calling the class member empty costructur |
64 | employee em1 = new employee(); |
65 | em1.display(); |
66 |
67 | } |
68 | } |
69 | } |
70 | } |
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 themain 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 font="">--ram>
i<--23 font="">--23>
sname<--ram font="">--ram>
sage<--23 font="">--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 font="">--shayam>
j<--33 font="">--33>
sname<--shyam font="">--shyam>
sage<--33 font="">--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 classagain 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.
Comments
Post a Comment