Part 29 - C# Tutorial - Difference between classes and structs in C#


Part 29 - C# Tutorial - Difference between classes and structs in C#


7 comments:

  1. Is it right to infer that elements on the stack is gets cleared on its own when the scope of the element is lost (value types) vs elements on the heap needs to be cleared by the garbage collector? Also how does a struct look like on a stack (i beleive its objects as well as object variable both are stored on stack only)

    ReplyDelete
    Replies
    1. You are right priyanaka. and to the 2nd part, yes, struct's variable and it's values are both stored on the stack and gets cleared, when the scope is lost.

      Delete
  2. 1. ID is an int type in 'Customer' class, since int is a value-type it shall store it on stack memory, rather heap memory. I'm bit confused in this context.
    If int ID stores on Heap Memory, then we can say value type can store on heap.

    2. Struct shall value type, on assuming there is a string variable defined in struct type, so this variable should store it on 'heap' memory since string is ref type. I shall want to know in this case string shall save in stack memory(bcoz it is present in struct) or heap memory (since string is ref type)

    ReplyDelete
  3. HI venkat,

    if your saying stack and heap means, Is it computer Ram memory virtually dividing into stack and heap right?
    if yes then how would computer knows the scope is lost then it has
    to clear value types(i.e variable and its value from stack) and also
    reference variable of reference type from stack memory implicitly?

    when do we use value types over reference types and what are the advantages?
    thanks venkat. I am waiting for reply, if there is any wrong plese correct me
    Thanks once again for grate effort and so far every thing was very precise

    ReplyDelete
  4. Hi Venkat,

    Recently i have came across with your blog.Every topic which you have posted here is very precise and every one can grab the technology easily.
    I have one doubt both classes and interfaces are reference types and struct is a value type right? then how struct is inheriting from interface and why not it is inheriting from other classes.As per this video interface is reference type and struct is value type.while inheriting from interface its has to throw error like implicit conversion not allowed or cannot be converted form reference to value type.But we are not getting any error.
    Pleas look into this and above question answer me.I am in bit confusion
    Thanks venkat grate effort.

    ReplyDelete
    Replies
    1. already explained in boxing and unboxing...converting value to reference type is boxing and vise versa is unboxing...

      Delete
  5. class Program
    {
    static void Main()
    {

    Customer C1=new Customer();
    C1.Id = 101;
    C1.Name = "marry";


    Customer C2 = new Customer();
    C2 = C1;
    C2.Name = "Justin";

    Console.WriteLine("C2.Name ={0} && C1.Name={1}", C2.Name, C1.Name);




    }
    }

    public struct Customer
    {
    private int _id;
    private string _name;

    public int Id
    {
    get;
    set;
    }

    public string Name
    {
    set;
    get;
    }
    }


    OUTPUT:-C2.Name =Justin && C1.Name=marry


    but as per the Rules of Class both C1 and C2 should be same right.

    Can some one please let me know where i am doing wrong.

    ReplyDelete

It would be great if you can help share these free resources