1
C# Structures: 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
● Book ID
Features of C# Structures:
● Structures can have methods, fields, indexers, properties, operator methods, and events.
● Structures can have defined constructors, but not destructors. However, you cannot define a
default constructor for a structure.
● Unlike classes, structures cannot inherit other structures or classes.
● Structures cannot be used as a base for other structures or classes.
● A structure can implement one or more interfaces.
● Structure members cannot be specified as abstract, virtual, or protected.
● When you create a struct object using the New operator, it gets created and the appropriate
constructor is called. Unlike classes, structs can be instantiated without using the New
operator.
● If the New operator is not used, the fields remain unassigned and the object cannot be used
until all the fields are initialized.
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new data
type, with more than one member for your program.
For example, 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
public class testStructure
{
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
public static void Main(string[] args)
Er. Niraj Karki
2
{
Books B1;
[Link] = "C Programming";
[Link] = "Ram kumar";
[Link] = "C Programming Tutorial";
B1.book_id = 6495407;
[Link]("Book 1 title : {0}", [Link]);
[Link]("Book 1 author : {0}", [Link]);
[Link]("Book 1 subject : {0}", [Link]);
[Link]("Book 1 book_id :{0}", B1.book_id);
}
}
OUTPUT:
Book 1 title : C Programming
Book 1 author : Ram kumar
Book 1 subject : C Programming Tutorial
Book 1 book_id :6495407
Same using class:
class Books
{
string title;
string author;
string subject;
int book_id;
static void Main(string[] args)
{
Books B1 = new Books();
[Link] = "C Programming";
[Link] = "Ram Kumar";
[Link] = "C Programming Tutorial";
B1.book_id = 6495407;
[Link]("Book 1 title : {0}", [Link]);
[Link]("Book 1 author : {0}", [Link]);
[Link]("Book 1 subject : {0}", [Link]);
[Link]("Book 1 book_id :{0}", B1.book_id);
}
}
OUTPUT:
Book 1 title : C Programming
Book 1 author : Ram Kumar
Book 1 subject : C Programming Tutorial
Book 1 book_id :6495407
P5.1 Write a c# program to input name address and roll no. of 5 students and display that
details using struct.
public class test
Er. Niraj Karki
3
{
struct Student
{
public string name;
public string address;
public int roll_no;
};
public static void Main(string[] args)
{
Student[] s = new Student[10];
for (int j = 1; j <= 5; j++)
{
[Link]("Student "+j);
[Link]("Enter name:");
s[j].name = [Link]();
[Link]("Enter address:");
s[j].address = [Link]();
[Link]("Enter Roll No.:");
s[j].roll_no = Convert.ToInt32([Link]());
}
[Link]("\nStudent Details:");
for (int j = 1; j <= 5; j++)
{
[Link]("Student {0}:", j);
[Link]("Name: " + s[j].name);
[Link]("Address: " + s[j].address);
[Link]("Roll No.:" + s[j].roll_no);
}
}
}
Difference between Class and Structure
Class Structure
Classes are of reference types. Structs are of value types.
Class has limitless features. Struct has limited features.
Class is generally used in large programs. Struct are used in small programs.
Classes can contain constructor or destructor. Structure does not contain parameter less constructor
or destructor, but can contain Parameterized
constructor or static constructor.
Classes used new keyword for creating Struct can create an instance, with or without new
instances. keyword.
A Class can inherit from another class. A Struct is not allowed to inherit from another struct
or class.
The data member of a class can be protected. The data member of struct can’t be protected.
Function member of the class can be virtual or Function member of the struct cannot be virtual or
abstract. abstract.
Er. Niraj Karki