Structure and Class in C#

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:-
using System;
namespace structure
    class Program
        public struct student
            public String sname;
            public int sage;
            public student(String s,int i)
                Console.WriteLine("student name is"+" " + sname);
                Console.WriteLine("student age is"+ " " + sage);
            public void show()
                Console.WriteLine("student name is" + " " + sname);
                Console.WriteLine("student age is" + " " + sage);
            public class employee
                public String ename;
                public int eage;

                public employee()
                    eage =24;
                public employee(String e, int j)
          Console.WriteLine("employee name(parameter department) is"+" " + ename);
          Console.WriteLine("employee age(parameter department) is" + " " + eage);
                public void display()
                    Console.WriteLine("employee name is" + " " + ename);
                    Console.WriteLine("employee age is" + " " + eage);
            static void Main(string[] args)
            {  //calling the structure member
                student st = new student("ram", 23);
                st.sname = "vinod";
                st.sage = 22;

                //calling the class member paramerized cosntructur
                employee em = new employee("shayam", 33);
                em.ename = "Rajesh";
                em.eage = 21;

                //calling the class member empty costructur
                employee em1 = new employee();


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.
  1. First i have created a structure student with struct keyword.
  2. 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.
     3. After that i have created a class employee.
     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.
student st=new student("ram",23);
here parameterized  constructor of student structure will call and copy the values in corresponding variables.



student name is ram
student age is 23

Now again ,i have assigned values in variable of student sname and sage with the help of object st.

here i have called the show() method of student to display the sname and sage values.

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 ).
employee em =new employee("shayam",33);
here parameterized  constructor of employee class will call and display the parameter values.



employee name(parameter department) is shayam
employee age(parameter department) is 33

Now again ,i have assigned values in variable of employee ename and eage with the help of object em.
here i have called the display() method of employee class to display the ename and eage values.
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();
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:-
I hope this helpful for you.
To Get the Latest  Free Updates Subscribe
please click below for download whole application.

1 comment:

Powered by Blogger.