Difference between Class and Structure in C#
Last Updated : 22 Sep, 2023
In C#, both classes and structures are used to define custom data types, but there are some differences between them.
Inheritance: A class can inherit from other classes, but a structure cannot. In other words, a class can be derived from another class, but a structure cannot be.
Reference type vs Value type: A class is a reference type, which means that when you create an object of a class, a reference to that object is created, and changes to the object are reflected wherever the reference is used. A structure, on the other hand, is a value type, which means that when you create a variable of a structure type, the actual value of the variable is stored in memory, and changes to the variable are not reflected elsewhere.
- Default Constructor: A class always has a default constructor that is called when an object of the class is created. A structure, on the other hand, does not have a default constructor by default. However, you can define a default constructor in a structure if you need one.
- Initialization: When you create an object of a class, its members are initialized to their default values (i.e., null for reference types and 0 for value types). When you create a variable of a structure type, its members are initialized to their default values as well. However, you can also provide initial values for the members of a structure when you create a variable of the structure type.
- Size and Performance: Structures are usually smaller in size than classes because they do not contain any reference variables or overhead. This means that structures can be faster to pass as parameters or to copy than classes.
A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member function which define actions) into a single unit.
Example:
CSharp
using System; public class Author { public string name; public string language; public int article_no; public int improv_no; public void Details( string name, string language, int article_no, int improv_no) { this .name = name; this .language = language; this .article_no = article_no; this .improv_no = improv_no; Console.WriteLine( "The name of the author is : " + name + "\nThe name of language is : " + language + "\nTotal number of article published " + article_no + "\nTotal number of Improvements:" + " done by author is : " + improv_no); } public static void Main(String[] args) { Author obj = new Author(); obj.Details( "Ankita" , "C#" , 80, 50); } } |
Output The name of the author is : Ankita The name of language is : C# Total number of article published 80 Total number of Improvements: done by author is : 50
A structure is a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types.
Example:
CSharp
using System; public struct Car { public string Brand; public string Model; public string Color; } class GFG { static void Main( string [] args) { Car c1; c1.Brand = "Bugatti" ; c1.Model = "Bugatti Veyron EB 16.4" ; c1.Color = "Gray" ; Console.WriteLine( "Name of brand: " + c1.Brand + "\nModel name: " + c1.Model + "\nColor of car: " + c1.Color); } } |
Output Name of brand: Bugatti Model name: Bugatti Veyron EB 16.4 Color of car: Gray
Difference between Class and Structure
Classes are of reference types. | Structs are of value types. |
All the reference types are allocated on heap memory. | All the value types are allocated on stack memory. |
Allocation of large reference type is cheaper than allocation of large value type. | Allocation and de-allocation is cheaper in value type as compare to reference type. |
Class has limitless features. | Struct has limited features. |
Class is generally used in large programs. | Struct are used in small programs. |
Classes can contain constructor or destructor. | Structure does not contain parameter less constructor or destructor, but can contain Parameterized constructor or static constructor. |
Classes used new keyword for creating instances. | Struct can create an instance, with or without new keyword. |
A Class can inherit from another class. | A Struct is not allowed to inherit from another struct or class. |
The data member of a class can be protected. | The data member of struct can’t be protected. |
Function member of the class can be virtual or abstract. | Function member of the struct cannot be virtual or abstract. |
Two variable of class can contain the reference of the same object and any operation on one variable can affect another variable. | Each variable in struct contains its own copy of data(except in ref and out parameter variable) and any operation on one variable can not effect another variable. |
In summary, the main differences between classes and structures in C# are inheritance, reference type vs value type, default constructor, initialization, and size/performance. Classes are usually used for larger, more complex objects, while structures are used for smaller, simpler objects that are used frequently and need to be passed around quickly. However, both classes and structures have their own strengths and weaknesses, and the choice between them ultimately depends on the specific requirements of the project.
Similar Reads
Difference between Abstract Class and Interface in C#
An abstract class is a way to achieve abstraction in C#. To declare an abstract class, we use the abstract keyword. An Abstract class is never intended to be instantiated directly. This class must contain at least one abstract method, which is marked by the keyword or modifier abstract in the class
4 min read
Difference between String and string in C#
String is an alias for System.String class and instead of writing System.String one can use String which is a shorthand for System.String class and is defined in the .NET base class library. The size of the String object in memory is 2GB and this is an immutable object, we can not modify the charact
2 min read
Difference Between List and Set in C#
The list is C# is the same as the list in JAVA. Basically, it is a type of object which can store variables. But in difference with objects, it stores the variables only in a specific order. Following is the syntax from which we can declare variables: Syntax: List<int> numbers = new List<in
2 min read
Difference between C and C#
C language: C language is a middle programming language that was developed at Bell research lab in 1972 by Dennis Ritchie. C language combines the properties of low level and high-level language. therefore its thought-about a middle programming Language. C may be a high-level classical kind programm
2 min read
Difference Between VB.NET and C#
Visual Basic .NET is a high-level programming language that was initially developed in 1991. It was the first programming language that directly supported programming graphical user interfaces using language-supplied objects. It supports all the concepts of an object-oriented such as object, class,
2 min read
Difference Between C# and ASP.NET
Pre-requisites: C#, ASP.NET C# (also known as C sharp) is an object-oriented programming language that is used to produce an array of applications for gaming, mobile, web, and Windows platforms also It is a modern and type-safe language and provides simple syntax which makes it easier to learn and i
2 min read
Difference Between Properties and Indexers in C#
Properties in C# are named members that use access modifiers to set and retrieve values of fields declared in a secured manner. Properties are used for abstracting and encapsulating access to a field of a class by defining only important actions and hiding their implementation. Properties are invoke
2 min read
Difference between Boxing and Unboxing in C#
Boxing and unboxing is an important concept in C#. C# Type System contains three data types: Value Types (int, char, etc), Reference Types (object) and Pointer Types. Basically, it converts a Value Type to a Reference Type, and vice versa. Boxing and Unboxing enables a unified view of the type syste
2 min read
C# | Difference between Static Constructors and Non-Static Constructors
Prerequisite: Constructors in C# Static constructors are used to initialize the static members of the class and are implicitly called before the creation of the first instance of the class. Non-static constructors are used to initialize the non-static members of the class. Below are the differences
6 min read
Difference between Method Overriding and Method Hiding in C#
Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called Method Overriding. In simple words, Overriding is a feature that allows
4 min read