Sunday 19 August 2018

C#: struts Vs classes

a. Struct is a value type and class is a reference type.
Let’s see it by an example.

Program.cs
using System;

class Employee
{
    private int id;
    private String firstName;
    private String lastName;

    public Employee(int id, String firstName, String lastName)
    {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

}

class Program
{
    static void printEmployee(Employee emp)
    {
        Console.WriteLine("id : {0}, firstName : {1}, lastName : {2}", emp.getId(), emp.getFirstName(), emp.getLastName());
    }

    static void changeEmployee(Employee emp)
    {
        emp.setId(12345);
        emp.setFirstName("John");
        emp.setLastName("Kurian");
    }

    static void Main(string[] args)
    {
        Employee emp1 = new Employee(1, "Hari krishna", "Gurram");

        printEmployee(emp1);
        changeEmployee(emp1);
        printEmployee(emp1);
    }
}

Output
id : 1, firstName : Hari krishna, lastName : Gurram
id : 12345, firstName : John, lastName : Kurian

Notify the method ‘changeEmployee(emp1);’ after calling changeEmployee method, object emp1 reflects the new values.

Now let’s update the ‘class Employee’ statement to ‘struct Employee’ and re run the application.


Program.cs

using System;

struct Employee
{
    private int id;
    private String firstName;
    private String lastName;

    public Employee(int id, String firstName, String lastName)
    {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

}

class Program
{
    static void printEmployee(Employee emp)
    {
        Console.WriteLine("id : {0}, firstName : {1}, lastName : {2}", emp.getId(), emp.getFirstName(), emp.getLastName());
    }

    static void changeEmployee(Employee emp)
    {
        emp.setId(12345);
        emp.setFirstName("John");
        emp.setLastName("Kurian");
    }

    static void Main(string[] args)
    {
        Employee emp1 = new Employee(1, "Hari krishna", "Gurram");

        printEmployee(emp1);
        changeEmployee(emp1);
        printEmployee(emp1);
    }
}

Output
id : 1, firstName : Hari krishna, lastName : Gurram
id : 1, firstName : Hari krishna, lastName : Gurram

Notify the output, after calling ‘changeEmployee’ method also, new values are not reflected. Since struct is value type, the changes are not reflected.

b. Struts are stored on stack, whereas classes are stored on heap.

c. Memory occupied by value types is freed immediately after it’s scope is lost, whereas memory occupied by reference types (Reference variable is deleted immediately after its scope is lost, whereas original object pointed by the reference variable is freed by garbage collector) is freed by garbage collector.

d. Copying the contents of a value type variable into another variable, copies the entire contents into the new variable, making the two distinct. In other words, after the copy, changes to one won't affect the other.


copying the contents of a reference type variable into another variable, copies the reference. In other words, after the copy, changing the data in one reference will appear to affect the other as well.



Previous                                                 Next                                                 Home

No comments:

Post a Comment