A structure in C# is simply a composite data type consisting of a number elements of other types. A C# structure is a value type and the instances or objects of a structure are created in stack. The structure in C# can contain fields, methods, constants, constructors, properties, indexers, operators and even other structure types.
Structure Declaration & Object Creation
The keyword struct can be used to declare a structure. The general form of a structure declaration in C# is as follows.
<modifiers> struct
<struct_name> { //Structure members }
Where the modifier can be private, public, internal or public. The struct is the required keyword.
For example
struct MyStruct {
public int x;
public int y;
}
The objects of a strcut can be created by using the new operator as follows.
MyStruct ms = new MyStruct();
The individual members of a struct can be accessed by using the dot (.) operator as showing below.
ms.x = 10;
ms.y = 20;
Remember that unlike classes, the strcut object can also be created without using the new operator.
MyStruct ms;
But in this case all fields of the struct will remain unassigned and the object can't be used until all of the fields are initialized.
Structs & Fields
A struct in C# can contain fields. These fields can be declared as private, public, internal. Remember that inside a struct, we can only declare a field. We can't initialize a field inside a struct. However we can use constructor to initialize the structure fields.
The following is not a valid C# struct and the code will not compile, since the fields inside the structure are trying to initialize.
struct MyStruct {
int x = 20; // Error its not possible to initialize
int y = 20; // Error its not possible to initialize
}
A valid C# structure is showing below.
// Author: [email protected]
using System;
struct MyStruct {
public int x;
public int y;
}
class MyClient {
public static void Main() {
MyStruct ms = new MyStruct();
ms.x = 10;
ms.y = 20;
int sum = ms.x + ms.y;
Console.WriteLine("The sum is {0}", sum);
}
}
However a struct can contain static fields, which can be initialized inside the struct. The following example shows the use of static fields inside a struct.
// Author: [email protected]
using System;
struct MyStruct {
public static int x = 25;
public static int y = 50;
}
class MyClient {
public static void Main() {
int sum = MyStruct.x + MyStruct.y;
Console.WriteLine("The sum is {0}", sum);
}
}
Remember that static fields can't be accessed by an instance of a struct. We can access them only by using the struct names.
Struct & Methods
A C# struct can also contain methods. The methods can be either static or non-static. But static methods can access only other static members and they can't invoke by using an object of the structure. They can invoke only by using the struct name.
An example is shown below.
// Author: [email protected]
using System;
struct MyStruct {
static int x = 25;
static int y = 50;
public void SetXY(int i, int j) {
x = i;
y = j;
}
public static void ShowSum() {
int sum = x + y;
Console.WriteLine("The sum is {0}", sum);
}
}
class MyClient {
public static void Main() {
MyStruct ms = new MyStruct();
ms.SetXY(100, 200);
MyStruct.ShowSum();
}
}
The methods inside a struct can also be overloaded as like inside a class. For example
// Author:[email protected]
using System;
struct MyStruct {
static int x = 25;
static int y = 50;
public void SetXY(int i, int j) {
x = i;
y = j;
}
public void SetXY(int i) {
x = i;
y = i;
}
}
class MyClient {
public static void Main() {
MyStruct ms1 = new MyStruct();
MyStruct ms2 = new MyStruct();
ms1.SetXY(100, 200);
ms2.SetXY(500);
}
}
Structs & Constructors
A C# struct can declare constrcutor, but they must take parameters. A default constructor (constructor without any parameters) are always provided to initialize the struct fields to their default values. The parameterized constructors inside a struct can also be overloaded.
Dilshad AshrafPosted Aug 18, 2020, 8:22 AM
Thank you, Great explanation.
Aditya KumarPosted Apr 8, 2017, 3:52 AM
Sir it seems there is type error at the bottom of the page, it should be "Heap" rather than "heal"?
Aditya KumarPosted Apr 8, 2017, 3:52 AM
Sir it seems there is type error at the bottom of the page, it should be "Heap" rather than "heal"?
Joe WilsonPosted Dec 29, 2015, 4:19 AM
Thank you very much.
JoshiPosted Dec 1, 2012, 5:04 AM
very nice one
vinod wattamwarPosted Oct 1, 2008, 7:13 AM
why we have to use new keyword while struct object .struct is value type still we are using new keyword
Anil Kumar AgrawalPosted Nov 21, 2007, 1:01 AM
You have written: ----------------- Structs & Properties The properties can be declared inside a struct as shown below. //C#: Property // Author: [email protected] using System; class MyStruct --------------------- Please change the 'class' keyword to 'struct'
Rohan MeditedPosted Jun 2, 2007, 9:49 AMEdited Jun 2, 2007, 10:02 AM
Can we assign the intger value in an object again and agin? using System; struct MyStruct { public int x = 25; public void SetX(int i) { x = i; } } class MyClient { public static void Main() { MyStruct ms = new MyStruct(); ms.SetX(100); // Store this object somewhere then again collect this object and // assign different value. ms.SetX(102); } }