Structures in C#
In C#, a structure is a value type data type. It helps you to make a single
variable hold related data of various data types. The struct keyword is
used for creating a structure.
Structures are used to represent a record. Suppose you want to keep track
of your books in a library. You might want to track the following
attributes about each book: - (Title, Author, Subject, and Book ID).
Defining a Structure
The keyword struct can be used to declare a structure. The general form
of a structure declaration in C# is as follows:
Where: the modifier can be private, public, internal or public.
The struct is the required keyword.
For example
1. struct MyStruct {
2. public int x;
3. public int y;
4. }
The objects of a strcut can be created by using the new operator as
follows.
1. MyStruct ms = new MyStruct();
The individual members of a struct can be accessed by using the dot (.)
operator as showing below.
1. ms.x = 10;
2. ms.y = 20;
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.
Remember that inside a struct, we can only declare a field. We can't
initialize a field inside a struct.
Example1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
struct MyStruct {
public int x;
public int y;
}
class Program
{
static void Main(string[] args)
{
MyStruct ms = new MyStruct();
ms.x = 10;
ms.y = 20;
int sum = ms.x + ms.y;
Console.WriteLine("The sum is {0}", sum);
}
}
}
Example2: here is the way you can declare the Book structure:
struct Books {
public string title;
public string author;
public string subject;
public int book_id;
};
The following program shows the use of the structure:-
using System;
struct Books {
public string title;
public string author;
public string subject;
public int book_id;
};
public class testStructure {
public static void Main(string[] args)
{
Books Book1; /* Declare Book1 of type Book */
Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
Book1.title = "C Programming";
Book1.author = "Nuha Ali";
Book1.subject = "C Programming Tutorial";
Book1.book_id = 6495407;
/* book 2 specification */
Book2.title = "Telecom Billing";
Book2.author = "Zara Ali";
Book2.subject = "Telecom Billing Tutorial";
Book2.book_id = 6495700;
/* print Book1 info */
Console.WriteLine( "Book 1 title : {0}", Book1.title);
Console.WriteLine("Book 1 author : {0}", Book1.author);
Console.WriteLine("Book 1 subject : {0}", Book1.subject);
Console.WriteLine("Book 1 book_id :{0}", Book1.book_id);
/* print Book2 info */
Console.WriteLine("Book 2 title : {0}", Book2.title);
Console.WriteLine("Book 2 author : {0}", Book2.author);
Console.WriteLine("Book 2 subject : {0}", Book2.subject);
Console.WriteLine("Book 2 book_id : {0}", Book2.book_id);
Console.ReadKey();
}
}
The output of the above code is:
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
Example3:
// 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 PG {
// 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
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 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 Prog1 {
// 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);
}// End of Main Method
} // End of class Prog1
}
Output:
Values Stored in p1
Name: Raman
Age: 12
City: ABC_City
State: XYZ_State