Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
Difference between String and string in C#
Next article icon

Difference between Class and Structure in C#

Last Updated : 22 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
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.



Next Article
Difference between String and string in C#
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp-OOP

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences