Abhishek Yadav
What are the Difference between Structure and Class??
By Abhishek Yadav in C# on Jul 27 2014
  • Pankaj  Kumar Choudhary
    Mar, 2015 27

    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
  • Abrar Ahmad Ansari
    Mar, 2015 10

    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
  • Rajeev Kumar
    Feb, 2023 27

    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

    • 0
  • latesh sarode
    Mar, 2021 2

    Struct vs Class

    struct Location
    {
    public int x, y;
    public Location(int x, int y)
    {
    this.x = x;
    this.y = y;
    }
    }
    Location a = new Location(20, 20);
    Location b = a;
    a.x = 100;
    System.Console.WriteLine(b.x)
    ;

    The output will be 20. Value of “b” is a copy of “a”, so “b” is unaffected by change of “a.x”. But in class, the output will be 100 because “a” and “b” will reference the same object.

    To answer this question, we should have a good understanding of the differences.

    1

    Structs are value types, allocated either on the stack or inline in containing types.

    Classes are reference types, allocated on the heap and garbage-collected.

    2

    Allocations and de-allocations of value types are in general cheaper than allocations and de-allocations of reference types.

    Assignments of large reference types are cheaper than assignments of large value types.

    3

    In structs, each variable contains its own copy of the data (except in the case of the ref and out parameter variables), and an operation on one variable does not affect another variable.

    In classes, two variables can contain the reference of the same object and any operation on one variable can affect another variable.

    • 0
  • Sandeep Singh
    Sep, 2016 5

    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.

    • 0
  • Ajay Gandhi
    Nov, 2015 20

    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.

    • 0
  • Yatendra Sharma
    Jul, 2015 8

    Structure for value type and class for reference type Structure stored in stack and class stored in the heap

    • 0
  • Lalit Raghav
    Apr, 2015 16

    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);}}

    • 0
  • Munesh Sharma
    Oct, 2014 9

    http://dotnet-munesh.blogspot.in/2013/12/difference.html

    • 0
  • Sunil Gaded
    Aug, 2014 28

    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

    • 0
  • varunkumar reddy
    Aug, 2014 23

    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

    • 0
  • Ankur Jain
    Aug, 2014 18

    http://www.codeproject.com/Articles/265755/Difference-between-Class-and-Structure-in-NET

    • 0
  • Abhishek Yadav
    Jul, 2014 27

    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.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS