Beruflich Dokumente
Kultur Dokumente
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:
1. struct MyStruct {
2. public int x;
3. public int y;
4. }
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;
46
C# Programming L3- Structures in C# Dr. Asmaa S., Dr.Bassim J., & Lectrurer Nada Th.
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.
Example1:
1. using System;
2. struct MyStruct {
3. public int x;
4. public int y;
5. }
6. class MyClient {
7. public static void Main() {
8. MyStruct ms = new MyStruct();
9. ms.x = 10;
10. ms.y = 20;
11. int sum = ms.x + ms.y;
12. Console.WriteLine("The sum is {0}", sum);
13. }
14. }
Example2: here is the way you can declare the Book structure:
struct Books {
};
46
C# Programming L3- Structures in C# Dr. Asmaa S., Dr.Bassim J., & Lectrurer Nada Th.
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);
44
C# Programming L3- Structures in C# Dr. Asmaa S., Dr.Bassim J., & Lectrurer Nada Th.
Example3:
// Defining structure
public struct Person
46
C# Programming L3- Structures in C# Dr. Asmaa S., Dr.Bassim J., & Lectrurer Nada Th.
{
// Declaring different data types
public string Name;
public int Age;
public int Weight;
class PG {
// Main Method
static void Main(string[] args)
{
// P1's data
P1.Name = "Keshav Gupta";
P1.Age = 21;
P1.Weight = 80;
46
C# Programming L3- Structures in C# Dr. Asmaa S., Dr.Bassim J., & Lectrurer Nada Th.
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;
}
46
C# Programming L3- Structures in C# Dr. Asmaa S., Dr.Bassim J., & Lectrurer Nada Th.
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);
Output:
Values Stored in p1
Name: Raman
Age: 12
City: ABC_City
State: XYZ_State
67