Tuesday 9 July 2013

Difference between Structure and Class in .NET || What are the difference between Structure and Class in .NET?

                           Hi friends in this article I will explain about the difference between Structure and Class.I think it may useful in interview Questions.I explained about the interviews questions in the previous articles Interview Questions in VB.NET, .NET Framework.ASP.NET interview Questions , Interview Questions on SQL Server , ASP.NET Interview Questions Part II and Interview Questions on SQL Server Part II

What are the difference between Structure and Class?
Class

Structure
Class is group of common objects that share common properties and relationships.
Structure is a collection of different types of data type.
Class Have memory overhead per new instance
Structure Do not have a memory overhead per new instance - unless 'boxed'
Class members are private by default.
Structure members are public by default.
Classes are reference type i.e. they are stored on heap
Structure are value type i.e. they are stored on stack
Class can be inherited.
Structure cannot be inherited.
Class is better choice for complex data structure
Structure should be used when you want to use a small data structure
Class elements can be declared as protected
Structure elements cannot be declared as protected
Classes are terminated i.e. GC does call finalize method for them
Structure are not terminated i.e. GC does not call finalize method for them
Class does require constructor.
Structure does not require constructor.

A structure must have always the default parameter less constructor be defined as public but a class might have one, so you can't define a private parameter less constructor


struct  Sample  {
        
private  Sample() // compile-time error        {
        }
    }
     
class
 Me         {
            
private  Sample() // runs Ok{
         }

See the below figure to know about the difference between reference type and value type.

Go to previous interview question what are the data access namespaces in .NET?
I think you like my blog why are waiting following me on facebook fan page Aspdotnet-roja

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.