What are the Difference between Structure and Class??
Abhishek Yadav
1. Class are ref type and Struc are value type 2. Struc doesn't support inheritance except interface. 3. Class use heap and Struc use Stacks 4 Class use concept of boxing and unboxing but Struc doesn't use 5. members of struc are by default public where as in class members are private. 6. we can not create default constructor of struc
1:-Strucuter is a value type and class is a reference type 2:-Classes will support an Inheritance whereas Structures won't 3:-Classes can have explicitly parameterless constructors whereas structures can't. 4:-Member variable initialization is possible in class whereas in Structures, it is not. 5:The “this” keyword gives different meaning in structure and class. How? In class, “this” keyword is classified as value type of class type within which it is used like inside instance constructor or instance method. In structure, “this” keyword is classified as variable type of structure type within which it is used.
1)Class use for representing entities with larger volume of data, Structure use for representing entities with Smaller volume of data. 2)In Class :- Memory is allocated on Managed Heap so automatic memory management is possible thru Garbage Collector, In Structure :- Store on Stack memory so automatic memory management is not possible thru Garbage Collector but "Faster in access.
Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static.
Structure for value type and class for reference type Structure stored in stack and class stored in the heap
This example help you difference between Class and Struct Analise out put of this console Application class A{private string _Height;private string _Weight;public string Height{get{return _Height;}set{_Height = value;}}public string Weight{get{return _Weight;}set{_Weight = value;}}}struct B{private string _Height;private string _Weight;public string Height{get{return _Height;}set{_Height = value;}}public string Weight{get{return _Weight;}set{_Weight = value;}}}class Test1{static void Main(string[] args){A objAOne = new A();Console.WriteLine("Property Height " + objAOne.Height + "Weight " + objAOne.Weight);objAOne.Height = "1";objAOne.Weight = "1";Console.WriteLine("Property Height " + objAOne.Height + "Weight " + objAOne.Weight);//obj.ChangeProperty();Console.WriteLine("Property Height " + objAOne.Height + "Weight " + objAOne.Weight);Console.WriteLine("----------------------Assign Value to Another objcet 'ObjSecond'-----------------------------------");A obASecond = objAOne;Console.WriteLine("----------------------Assign Value to objcet 'ObjSecond' 3 & 3-----------------------------------");obASecond.Height = "3";obASecond.Weight = "3";Console.WriteLine("----------------------Call to First object-----------------------------------");Console.WriteLine("Property Height " + objAOne.Height + "Weight " + objAOne.Weight);Console.WriteLine("----------------------Call to Second object-----------------------------------");Console.WriteLine("Property Height " + obASecond.Height + "Weight " + obASecond.Weight);Console.WriteLine("-------------------------Struct---------------------------------------");B objBOne = new B();Console.WriteLine("Property Height " + objBOne.Height + "Weight " + objBOne.Weight);objBOne.Height = "1";objBOne.Weight = "1";Console.WriteLine("Property Height " + objBOne.Height + "Weight " + objBOne.Weight);// objB.ChangeProperty();Console.WriteLine("Property Height " + objBOne.Height + "Weight " + objBOne.Weight);Console.WriteLine("----------------------Assign to Second object-----------------------------------");B objBSecond = objBOne;Console.WriteLine("----------------------Assign to Second object-----------------------------------");Console.WriteLine("----------------------Assign Value to objcet 'ObjSecond' 3 & 3-----------------------------------");objBSecond.Height = "3";objBSecond.Weight = "3";Console.WriteLine("----------------------Call first obj-----------------------------------");Console.WriteLine("Property Height " + objBOne.Height + "Weight " + objBOne.Weight);Console.WriteLine("----------------------Call second obj-----------------------------------");Console.WriteLine("Property Height " + objBSecond.Height + "Weight " + objBSecond.Weight);}}
http://dotnet-munesh.blogspot.in/2013/12/difference.html
Stru 1.values types,so stored on stack 2.with or without using new keyword we can create the object of the stru's. 3.it consists only construters not destucter. 4.inside the no initilization takes place. 5.not possible to inhrit another struct but from interface possible
class Structure 1.class has all access 1.structures cannot have protected and internal modifiers protected 2.Value type 2.reference type 3.supports inheritance 3.doesnot support inheritance
http://www.codeproject.com/Articles/265755/Difference-between-Class-and-Structure-in-NET
1. Structure is Value Type. Class is Reference Type. 2. Structure is stored on Stack. Class is stored on Heap. 3. Structure does not support Inheritance. Class supports Inheritance. 4. 'this' pointer does not supported by Structure. 'this' pointer is only supported by Classes. 5. Structure do not require Constructor. Classes require Constructors. 6. Only Interface Inheritance is possible in Structure. 7. Default access specifier for Structure is public For Class, default access specifier is private.