Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

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

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


  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)

    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.

  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)

  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

  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.


If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.