Open In App

C# | Structures | Set – 1

Last Updated : 27 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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: 
     

CSHARP




// C# program to illustrate the
// Declaration of structure
using System;
namespace ConsoleApplication {
 
// Defining structure
public struct Person
{
    // Declaring different data types
    public string Name;
    public int Age;
    public int Weight;
 
}
 
class Geeks {
     
    // Main Method
    static void Main(string[] args)
    {
 
        // Declare P1 of type Person
        Person P1;
 
        // P1's data
        P1.Name = "Keshav Gupta";
        P1.Age = 21;
        P1.Weight = 80;
 
        // Displaying the values
        Console.WriteLine("Data Stored in P1 is " +
                           P1.Name + ", age is " +
                           P1.Age + " and weight is " +
                           P1.Weight);
 
    }
}
}


  •  
Output: 

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:
     

CSHARP




// C# program to illustrate copy the structure
using System;
namespace ConsoleApplication {
 
// Defining structure
public struct Person
{
    // Declaring different data types
    public string Name;
    public int Age;
    public int Weight;
 
}
 
class Geeks {
     
    // Main Method
    static void Main(string[] args)
    {
 
        // Declare P1 of type Person
        Person P1;
 
        // P1's data
        P1.Name = "Keshav Gupta";
        P1.Age = 21;
        P1.Weight = 80;
         
        // Declare P2 of type Person
        Person P2;
         
        // Copying the values of P1 into P2
         P2 = P1;
 
        // Displaying the values of P1
        Console.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 P2
        Console.WriteLine("Values Stored in P2");
        Console.WriteLine("Name: " +P2.Name);
        Console.WriteLine("Age: " +P2.Age);
        Console.WriteLine("Weight: " +P2.Weight);
                            
    }
}
}


  •  
Output: 

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 initialized 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:
     

CSHARP




// C# program to illustrate Nesting of structures
using System;
namespace ConsoleApplication {
 
// first structure defined
// with public modifier
public struct Address
{
     
    // data member of Address structure
    public string City;
    public string State;
}
 
 
// Another structure
struct Person
{
     
    // data member of Person structure
    public string Name;
    public int Age;
     
    // Nesting of Address structure
    // by creating A1 of type Address
    public Address A1;
}
 
class Geeks {
     
    // Main method
    static void Main(string[] args)
    {
 
        // Declare p1 of type Person
        Person p1;
 
        // Assigning values to the variables
        p1.Name = "Raman";
        p1.Age = 12;
         
        // Assigning values to the nested
        // structure data members
        p1.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);
 
    }
}
}


  •  
Output: 

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

 



Similar Reads

C# Program to Demonstrate the Array of Structures
An array of structures means each array index contains structure as a value. In this article, we will create the array of structure and access structure members using an array with a specific index. Syntax array[index].Structure(Details); where index specifies the particular position to be inserted and details are the values in the structure Exampl
2 min read
How to set the foreground color of the CheckBox in C#?
The CheckBox control is the part of windows form which is used to take input from the user. Or in other words, CheckBox control allows us to select single or multiple elements from the given list. In CheckBox, you are allowed to set the foreground color of the CheckBox using the ForeColor property of the CheckBox. It makes your CheckBox more attrac
3 min read
Array.GetValue() Method in C# with Examples | Set - 1
Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32) Array.GetValue(Int64, Int64) Array.GetValue(Int32) Array.GetValue(Int64) Array.GetValue(Int32, Int32, Int32) Array.GetValue(Int64, Int
4 min read
How to set the Visibility of the Button in C#?
A Button is an essential part of an application, or software, or webpage. It allows the user to interact with the application or software. In Button, you are allowed to set a value which represents the button and its child buttons are displayed by using the Visible Property. It is provided by Button class. If you want to display the given button an
3 min read
How to set the Alignment of the Text in RadioButton in C#?
In Windows Forms, RadioButton control is used to select a single option among the group of the options. For example, select your gender from the given list, so you will choose only one option among three options like Male or Female or Transgender. In Windows Forms, you are allowed to adjust the alignment of the RadioButton using the TextAlign Prope
3 min read
How to set Text in the RadioButton in C#?
In Windows Forms, RadioButton control is used to select a single option among the group of the options. For example, select your gender from the given list, so you will choose only one option among three options like Male or Female or Transgender. In Windows Forms, you are allowed to add text in the RadioButton using the Text Property of the RadioB
3 min read
How to set the Size of the RadioButton in C#?
In Windows Forms, RadioButton control is used to select a single option among the group of the options. For example, select your gender from the given list, so you will choose only one option among three options like Male or Female or Transgender. In Windows Forms, you are allowed to adjust the size of the RadioButton using the Size Property of the
3 min read
How to set the Font of the ListBox in C#?
In Windows Forms, ListBox control is used to show multiple elements in a list, from which a user can select one or more elements and the elements are generally displayed in multiple columns. In ListBox, you are allowed to set the font of the content present in the ListBox using Font Property of the ListBox which makes your ListBox more attractive.
3 min read
How to set the Alignment of the Text in the TextBox in C#?
In Windows forms, TextBox plays an important role. With the help of TextBox, the user can enter data in the application, it can be of a single line or of multiple lines. In TextBox, you are allowed to set the alignment of the text present in the TextBox by using the TextAlign Property of the TextBox. The default value of this property is Horizontal
3 min read
C# | Check if two String objects have the same value | Set-1
String.Equals Method method is used to check whether the two String objects have the same value. This method can be overloaded by passing different numbers and types of parameters to it. There are total 5 methods in the overload list of this method in which the first 2 are discussed in this article and the remaining are discussed in Set-2 and Set-3
3 min read
Article Tags :