Structure is a value type and 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. C# provide the ability to use pre-defined data types. However, sometimes the user might be in need to define its own data types which are also known as User-Defined Data Types. Although it comes under the value type, the user can modify it according to requirements and that’s why it is also termed as the user-defined data type.
Defining Structure: In C#, structure is defined using struct keyword. Using struct keyword one can define the structure consisting of different data types in it. A structure can also contain constructors, constants, fields, methods, properties, indexers and events etc.
- Syntax:
Access_Modifier struct structure_name { // Fields // Parameterized constructor // Constants // Properties // Indexers // Events // Methods etc. } - Example:
// C# program to illustrate the// Declaration of structureusingSystem;namespaceConsoleApplication {// Defining structurepublicstructPerson{// Declaring different data typespublicstringName;publicintAge;publicintWeight;}classGeeks {// Main MethodstaticvoidMain(string[] args){// Declare P1 of type PersonPerson P1;// P1's dataP1.Name ="Keshav Gupta";P1.Age = 21;P1.Weight = 80;// Displaying the valuesConsole.WriteLine("Data Stored in P1 is "+P1.Name +", age is "+P1.Age +" and weight is "+P1.Weight);}}}chevron_rightfilter_noneOutput:Data Stored in P1 is Keshav Gupta, age is 21 and weight is 80
Explanation: In the above code, a structure with name “Person” is created with data members Name, Age and Weight.In the main method, P1 of structure type Person is created. Now, P1 can access its data members with the help of .( dot ) Operator.
Copy Structure: In C#, user can copy one structure object into another one using ‘=’ (Assignment) operator.
- Syntax:
Structure_object_destination = structure_object_source;
- Example:
// C# program to illustrate copy the structureusingSystem;namespaceConsoleApplication {// Defining structurepublicstructPerson{// Declaring different data typespublicstringName;publicintAge;publicintWeight;}classGeeks {// Main MethodstaticvoidMain(string[] args){// Declare P1 of type PersonPerson P1;// P1's dataP1.Name ="Keshav Gupta";P1.Age = 21;P1.Weight = 80;// Declare P2 of type PersonPerson P2;// Copying the values of P1 into P2P2 = P1;// Displaying the values of P1Console.WriteLine("Values Stored in P1");Console.WriteLine("Name: "+P1.Name);Console.WriteLine("Age: "+P1.Age);Console.WriteLine("Weight: "+P1.Weight);Console.WriteLine("");// Displaying the values of P2Console.WriteLine("Values Stored in P2");Console.WriteLine("Name: "+P2.Name);Console.WriteLine("Age: "+P2.Age);Console.WriteLine("Weight: "+P2.Weight);}}}chevron_rightfilter_noneOutput:
Values Stored in P1 Name: Keshav Gupta Age: 21 Weight: 80 Values Stored in P2 Name: Keshav Gupta Age: 21 Weight: 80
Explanation: The data members of struct Person is intialized with the help of P1 and the values of data members can be copy to P2 by P1 using ‘='(assignment operator).
Nesting of Structures: C# allows the declaration of one structure into another structure and this concept is termed as the nesting of the structure.
- Example:
// C# program to illustrate Nesting of structuresusingSystem;namespaceConsoleApplication {// first structure defined// with public modifierpublicstructAddress{// data member of Address structurepublicstringCity;publicstringState;}// Another structurestructPerson{// data member of Person structurepublicstringName;publicintAge;// Nesting of Address structure// by creating A1 of type AddresspublicAddress A1;}classGeeks {// Main methodstaticvoidMain(string[] args){// Declare p1 of type PersonPerson p1;// Assigning values to the variablesp1.Name ="Raman";p1.Age = 12;// Assigning values to the nested// structure data membersp1.A1.City ="ABC_City";p1.A1.State ="XYZ_State";Console.WriteLine("Values Stored in p1");Console.WriteLine("Name: "+p1.Name);Console.WriteLine("Age: "+p1.Age);Console.WriteLine("City: "+p1.A1.City);Console.WriteLine("State: "+p1.A1.State);}}}chevron_rightfilter_noneOutput:Values Stored in p1 Name: Raman Age: 12 City: ABC_City State: XYZ_State
Important Points about Structures:
- Once the structures go out of scope, it gets automatically deallocated.
- Created much more easily and quickly than heap types.
- Using structure it become easy to copy the variable’s values onto stack.
- A struct is a value type, whereas a class is a reference type.
Difference Between Structures and Class :
| Category | Structure | Class |
|---|---|---|
| Data Type | Value Type | Reference type |
| Assignment Operation | Copies the value | Copies the reference |
| Parameterless Constructors | Not Allowed | Allowed |
| Inheritance | Not supported | Always supported |
Recommended Posts:
- How to set the foreground color of the CheckBox in C#?
- Array.GetValue() Method in C# with Examples | Set - 1
- How to set the Visibility of the Button in C#?
- How to set the Alignment of the Text in RadioButton in C#?
- How to set Text in the RadioButton in C#?
- How to set the Size of the RadioButton in C#?
- How to set the Font of the ListBox in C#?
- How to set the Alignment of the Text in the TextBox in C#?
- C# | Check if two String objects have the same value | Set-1
- C# | Graphics.DrawLine() Method | Set - 1
- How to use Array.BinarySearch() Method in C# | Set -1
- How to set the Location of the TextBox in C#?
- C# | String.IndexOf( ) Method | Set - 1
- C# | Join() Method | Set - 1
- C# | Join Method | Set - 2
- C# | Math.Round() Method | Set - 1
- How to set a Check Box in the DateTimePicker in C#?
- How to set the Name of the RadioButton in C#?
- C# | Math.Round() Method | Set – 2
- How to set the Visibility of the RadioButton in C#?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

