C# Struct vs Class

WBOY
Release: 2024-09-03 15:07:59
Original
717 people have browsed it

As the name says, C# uses ‘struct’ keyword to define the value types, and Class uses ‘class’ keyword to define the reference types. In C#, the variables defined in the structure are stored in stack or in the given variable type and the instances are called as structure variable. Whereas, for a ‘class’ the instances are called as objects and are stored in a heap structured memory units. In terms of constructors and destructors, C# struct cannot have a destructor, but a Class can have a destructor. Member classification is allowed in the Class, such as abstract, virtual and protected types.

Head to Head Comparison Between C# Struct vs Class (Infographics)

Below are the top 14 differences between C# Struct vs Class.

C# Struct vs Class

Key Differences Between C# Struct vs Class

Some key differences between C# Struct and Class are as follows:

  1. A struct can be declared using ‘struct’ keyword whereas a class can be declared using ‘class’ keyword.
  2. The struct is a value type thus a struct types variable directly consists of the struct data whereas class is a reference type and class type variable consists of a reference to the data, which we called an object of the class.
  3. Class type variables are allocated in heap and can be garbage collected whereas struct type variables are allocated either in the stack or inline in containing type.
  4. A class object is created using a ‘new’ keyword whereas the object of structure can be created with or without ‘new’ keyword. Instantiating struct without using a ‘new’ operator does not allow the user to access its methods, properties or events.
  5. Each variable in struct contains its copy of data (except ref and out parameter variables) so that modifications performed on one variable do not affect another variable whereas in class two or more variables can refer to the same object and any modifications performed on one variable can affect another variable. We can understand this point with the below examples.

Example using struct

Code:

using System;
namespace structAndClass
{
//creating structure
public struct Demo
{
public int x, y;
//parameterized constructor
public Demo(int x, int y)
{
this.x = x;
this.y = y;
}
}
public class StructDemo
{
public static void Main(string[] args)
{
Demo a = new Demo(50, 50);
Demo b = a;
a.x = 100;
Console.WriteLine("Value of a.x = "+a.x);
Console.WriteLine("Value of b.x = "+b.x);
}
}
}
Copy after login

Output:

C# Struct vs Class

Example using class

Code:

using System;
namespace structAndClass
{
public class Demo
{
public int x, y;
public Demo(int x, int y)
{
this.x = x;
this.y = y;
}
}
public class StructDemo
{
public static void Main(string[] args)
{
Demo a = new Demo(50, 50);
Demo b = a;
a.x = 100;
Console.WriteLine("Value of a.x = "+a.x);
Console.WriteLine("Value of b.x = "+b.x);
}
}
}
Copy after login

Output:

C# Struct vs Class

  1. Allocation and de-allocation of memory for struct type is cheaper than that of class type.
  2. A struct cannot have a parameterless instance constructor, it can have parameterized or static constructor whereas a class can have default parameterless constructor in it.
  3. A struct cannot have a destructor whereas a class can have a destructor.
  4. We cannot inherit a struct from another struct or class and it cannot be the base of a class whereas we can inherit a class from another class and a class can be the base of another class. Thus, class supports inheritance whereas structure does not support inheritance.
  5. We cannot specify the members of a structure as abstract, virtual or protected whereas a class can have its members specified as abstract, virtual or protected.
  6. The instance of the class is called an object whereas the instance of a structure is called the structure variable.
  7. If we have not specified any access specifier then the members of a class are private by default whereas the members of a structure will be public by default.
  8. Class is used for complex data structure whereas structure is used for the small data structure.

C# Struct vs Class Comparison Table

Let’s see some more differences between C# Struct vs Class through a comparison table for clear understanding:

Parameter C# Struct Class
Data type The structure is a value type of data type. Class is a reference type data type.
Keyword The structure can be defined using the ‘struct’ keyword. The class can be defined using the ‘class’ keyword.
Storage area The structure variable is stored either in stack or inline in containing type. The object of the class is stored in heap.
Instance creation The instance of a struct can be created with or without a ‘new’ keyword. The instance of the class is created using a ‘new’ keyword.
Assignment If we make an assignment to a variable of struct type then it creates a copy of the value being assigned. Thus, each variable instructs has its copy of data. If we make an assignment to a variable of class type then it copies the reference. Thus, two or more variables in class can refer to the same object.
Constructor The structure does not contain a parameterless instance constructor. The class contains a parameterless instance constructor, if not defined explicitly by the user.
Destructor It cannot have a destructor. It can have a destructor.
Inheritance The structure cannot inherit from another class or structure.

The structure cannot be used as a base for another structure or class.

The class can inherit from another class. The class can be a base class for another class.
Data members Members of a struct cannot be abstract, virtual or protected. Members of a class can be abstract, virtual or protected.
Instance The instance of the structure is called the structure variable. The instance of the class is called an object.
Default access specifier Members of the structure are public by default. Members of the class are private by default.
Usage The structure can be used for small data structures. Class is used for the complex data structure.
Garbage collection The instance of a struct cannot be garbage collected. The instance of the class can be garbage collected.
Variable initializing Including variable initializer is not allowed while making instance field declarations instruct. Including variable initializer is allowed while making instance field declarations in a class.

Conclusion

Both class and structure are used to hold related data items. We can use class for a large number of data and can use the structure for small data. Class is of reference type and stored in heap whereas structure is of value type and stored in the stack.

The above is the detailed content of C# Struct vs Class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!