Open In App

Difference between Class and Structure in C#

Last Updated : 22 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  1. 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.
  2. 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.
  3. 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




// C# program to illustrate the
// concept of class
using System;
 
// Class Declaration
public class Author {
 
    // Data members of class
    public string name;
    public string language;
    public int article_no;
    public int improv_no;
 
    // Method of class
    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);
    }
 
    // Main Method
    public static void Main(String[] args)
    {
 
        // Creating object
        Author obj = new Author();
 
        // Calling method of class
        // using class object
        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




// C# program to illustrate the
// concept of structure
using System;
 
// Defining structure
public struct Car
{
 
    // Declaring different data types
    public string Brand;
    public string Model;
    public string Color;
}
 
class GFG {
 
    // Main Method
    static void Main(string[] args)
    {
 
        // Declare c1 of type Car
        // no need to create an
        // instance using 'new' keyword
        Car c1;
 
        // c1's data
        c1.Brand = "Bugatti";
        c1.Model = "Bugatti Veyron EB 16.4";
        c1.Color = "Gray";
 
        // Displaying the values
        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

Class

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads