In c#, a structure refers to a user-defined value type. It allows you to create new value-type objects that are similar to the built-in type objects, such as int, double, and bool. A structure can contain constructors, fields, methods, and properties. One major difference between a structure and a class in c# is that a structure does not support inheritance. By default, all the members of a structure are private. If you do not assign a value to a variable of a structure, a default value is automatically assigned to the variable.
The struct keyword is used to declare a structure in c#. The basic syntax to define a structure is as follows:
In C#, use a struct when:
- You have a small amount of data to store and want to avoid the overhead of a class.
- The data is logically grouped and has a clear meaning as a single unit.
- The data is value-based, meaning that when the struct is assigned to a variable, a copy of the data is created, rather than a reference to the original.
Use a class when:
- You have a complex data structure with methods and behavior.
- The data is reference-based, meaning that you want to store a reference to an object rather than creating a new copy each time it is assigned to a variable.
- You need inheritance and polymorphism, which are not available with structs.
[access-modifier] struct <structure-name>
{
Vaiable-declaration;
Method-declarations;
}
In the preceding syntax,
struct is a keyword indicating that a structure is defined and
structure-name is the name of the structure begging defined.
using System;
namespace Structures
{
public struct X
{
public int a,b,c;
}
public class Program
{
public static void Main()
{
X X1= new X();
X1.a=1;
X1.b=2;
X1.c=3;
Console.WriteLine("Data of the X1 object:");
Console.WriteLine("A ="+ X1.a);
Console.WriteLine("B ="+ X1.b);
Console.WriteLine("C ="+X1.c);
X X2= new X();
Console.WriteLine("Data of the X2 object:");
Console.WriteLine("a = "+X2.a);
Console.WriteLine("b = "+X2.b);
Console.WriteLine("c = "+X2.c);
Console.ReadLine();
}
}
}
The first code snippet above shows the definition of
structures. The X structure contains three int-type variables, a, b, and c.In the
second code snippet, we have created an object X1, of the X structure and
assigned the values 1,2, and 3 to the a;b, and c, variables of the X1 object,
respectively. After that, we displayed the values of all three variables, a,b, and c, on the screen .Net, we created another object, X2 of the X
structure, and also displayed the values of all the three variables, a, b, and
c, of the X1 object on the screen.
8. Press the F5 key on the keyboard to run the application.
How to use struct in c#
Here's a simple example of how to use a struct in C#:
public struct Point
{
public int X;
public int Y;
public Point(int x, int y)
{
X = x;
Y = y;
}
}
class Program
{
static void Main(string[] args)
{
Point point = new Point(10, 20);
Console.WriteLine("Point: ({0}, {1})", point.X, point.Y);
}
}
In this example, the Point
struct has two public fields X
and Y
to store the coordinates of a point in two-dimensional space. The Point
struct also has a constructor to initialize its fields with values. The Main
method creates a new Point
struct and displays its values.
Note that when a struct is assigned to a variable, a copy of the struct is created, rather than a reference to the original. This is why structs are commonly used for small data structures that represent values.
C# Struct vs Class
C# struct
and class
are two data types used to define custom data structures. Here are some of the key differences between them:
Value vs Reference Types: Structs are value types, while classes are reference types. This means that when you assign a struct to a variable, a new copy of the struct is created, whereas when you assign a class to a variable, a reference to the same instance of the class is stored in the variable.
Memory Allocation: Structs are stored on the stack, which is a region of memory used for temporary data storage. Classes are stored on the heap, which is a region of memory used for long-term data storage.
Default Constructor: Structs have a default constructor, which initializes all fields to their default values. Classes do not have a default constructor, so you must explicitly create a constructor if you want to initialize fields.
Inheritance: Classes support inheritance, which allows you to define a new class based on an existing class. Structs do not support inheritance.
Polymorphism: Classes support polymorphism, which allows you to use objects of different classes through a common interface. Structs do not support polymorphism.
Size: Structs have a maximum size of 16 bytes, whereas classes have no size limit.
When deciding whether to use a struct or a class, consider the size of the data, the expected lifetime of the data, and the behavior you want to associate with the data. Use structs for small data structures with a short lifetime, and use classes for complex data structures with a longer lifetime.
struct contents reference from a non-struct array object.
In C#, it's possible to store a reference to a struct inside an array of reference types, such as an array of classes. However, this does not change the nature of the struct as a value type. The struct itself is still stored on the stack, and when it is accessed through the reference stored in the array, a copy of the struct is created.
Here's an example:
public struct Point
{
public int X;
public int Y;
}
class Program
{
static void Main(string[] args)
{
Point[] points = new Point[1];
points[0].X = 10;
points[0].Y = 20;
Console.WriteLine("Point: ({0}, {1})", points[0].X, points[0].Y);
}
}
In this example, the Point
struct is stored in an array of reference types, points
. However, when the Point
struct is accessed through the reference stored in the array, a copy of the struct is created and its values are displayed.
It's important to understand that this does not change the behavior of the struct as a value type. When the Point
struct is assigned to a new variable, a new copy of the struct is created, and when a method is called on the struct, a copy of the struct is passed to the method.
Comments