A structure is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events and nested types. A structure can be defined using the struct keyword, so A struct is a value type so it is faster than a class object. A structure in C# is simply a composite data type consisting of many elements of other types.
Example:- 1
using System;
public struct Students
{
public string name;
public string fatherName;
public string cource;
public int roll_No;
};
public class testStructureClass
{
public static void Main(string[] args)
{
Students std1; /* Declare Student 1 of
type students */
Students std2; /* Declare Student 2 of
type students */
/* Student 1 detials */
std1.name = "Nitin Kumar ";
std1.fatherName = "LAL BAHADUR";
std1.cource = "MCA";
std1.roll_No = 1155445;
/* student 2 details */
std2.name = "RAHUL KUMAR";
std2.fatherName = "LAL BAHADUR";
std2.cource = "BCA";
std2.roll_No = 665897;
/* print Student 1 details */
Console.WriteLine("Student 1 name : {0}", std1.name);
Console.WriteLine("Student 1 fatherName : {0}", std1.fatherName);
Console.WriteLine("Student 1 cource : {0}", std1.cource);
Console.WriteLine("Student 1 roll_No :{0}", std1.roll_No);
/* print student 2 info */
Console.WriteLine("Student 2 name : {0}", std2.name);
Console.WriteLine("Student 2 fatherName : {0}", std2.fatherName);
Console.WriteLine("Student 2 cource : {0}", std2.cource);
Console.WriteLine("Student 2 roll_NO : {0}", std2.roll_No);
Console.ReadKey();
}
}
|
Output:-
Student 1 name : Nitin Kumar
Student 1 fatherName : LAL BAHADUR
Student 1 cource : MCA
Student 1 roll_No :1155445
Student 2 name : RAHUL KUMAR
Student 2 fatherName : LAL BAHADUR
Student 2 cource : BCA
Student 2 roll_NO : 665897
|