C# | Structures | Set - 1
Last Updated :
27 Jun, 2021
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.
Access_Modifier struct structure_name
{
// Fields
// Parameterized constructor
// Constants
// Properties
// Indexers
// Events
// Methods etc.
}
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.
Structure_object_destination = structure_object_source;
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.
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
Structures in C++ C++ Structures are used to create user defined data types which are used to store group of items of different data types.SyntaxBefore using structure, we have to first define the structure using the struct keyword as shown:C++struct name{ type1 mem1; type2 mem2; ... };where structure name is name an
8 min read
set::size() in C++ STL In C++, set::size() function is a built-in used to find the number of elements in the given set container. It is the member function of std::set class defined inside <set> header file. In this article, we will learn about the std::set::size() method in C++.Example:C++// C++ Program to illustra
2 min read
set::clear in C++ STL Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::clear() clear()
2 min read
Difference Between Structure and Union in C In C programming, both structures and unions are used to group different types of data under a single name, but they behave in different ways. The main difference lies in how they store data.The below table lists the primary differences between the C structures and unions:ParameterStructureUnionDefi
4 min read
unordered_set clear() function in C++ STL The unordered_set::clear() function is a built-in function in C++ STL which is used to clear an unordered_set container. That is, this function removes all of the elements from an unordered_set and empties it. All of the iterators, pointers, and references to the container are invalidated. This redu
2 min read