Class 12 | Unit 5 | Structures | Visual Programming Notes

5.1 Introduction to Structure

In C#, a structure is a user-defined value type that is used to group related data members of different types under a single name. It allows us to create a composite data type, which can be used to store and manage data more effectively.

Structures are similar to classes, but they are value types, not reference types. They are mainly used to represent small data objects, such as a point, a date, a student record, etc. Example: A structure can be used to represent a point in a 2D coordinate system with x and y coordinates.

struct Point
{
public int x;
public int y;
}
Here, Point is a structure that contains two integer fields: x and y. 

5.2 Defining a Structure

To define a structure in C#, we use the keyword struct followed by the name of the structure and the list of data members enclosed in curly braces { }. The data members can be of any valid data type, including primitive types, strings, arrays, or even other structures.

Syntax:

struct StructureName
{
data-type member1;
data-type member2;
...
}

Example:

struct Student
{
    public string Name;
    public int RollNo;
    public int Age;
}
In this example, Student is a structure with three data members: Name, RollNo, and Age.







5.3 Features of C# Structure

1. Value Type:
A structure is a value type in C#. It stores the actual data directly rather than storing a reference to the data. When one structure variable is assigned to another, a separate copy of the value is created.

2. User-defined:
A structure is a user-defined data type created by the programmer. We can define our own data members and methods inside a structure. It is useful for grouping related data into a single meaningful unit.

3. Multiple Data Members:
A structure can contain multiple data members of different data types. For example, it can contain int, float, char, and string members. This allows related information to be stored together in one structure.

4. No Inheritance:
A structure cannot inherit from another structure or class. It also cannot be used as a base type for another class or structure. However, a structure can implement one or more interfaces.

5. No Default Constructor:
A programmer cannot explicitly declare a parameterless instance constructor in a structure. C# automatically provides default initialization for structure values. However, parameterized constructors can be defined to initialize specific values.

6. Can Implement Interfaces:
A structure can implement one or more interfaces. It can provide the methods and properties required by those interfaces. This allows structures to provide common behavior without using class inheritance.

7. Can Contain Methods:
A structure can contain methods, properties, indexers, and operators. These members can perform different operations on the data stored in the structure. Therefore, a structure can contain both data and related functionality.

8. Lightweight:
Structures are generally suitable for small and simple data objects. They can have less object-management overhead than classes in appropriate situations. However, their performance depends on how they are designed and used.

9. Used in Interoperability:
Structures are commonly used for interoperability with unmanaged or native code. They can represent data layouts required by operating-system APIs and other native libraries. This makes them useful when C# programs communicate with external system-level code.

 





5.4 Class vs. Structure in Visual Programming

In C#, class and structure are both used to combine related data and methods. The major differences between them are:

ClassStructure
A class is a reference type.A structure is a value type.
Class objects are generally stored on the heap.Structure values are stored directly as values.
A class can inherit from another class.A structure cannot inherit from another class or structure.
A class can implement one or more interfaces.A structure can also implement one or more interfaces.
A class can have constructors and a finalizer.A structure can have constructors but cannot have a finalizer.
Assigning one class variable to another copies the reference.Assigning one structure variable to another normally copies the value.
A class variable can contain null.A structure variable normally cannot contain null.
Classes are suitable for large and complex objects.Structures are suitable for small and simple data objects.
Example: class Student { }Example: struct Point { }

5.5 Declaring and Initializing a Structure
Syntax:

struct StructureName
{
// fields (data members)
// methods (member functions)
}

Example:

struct Student
{
public string Name;
public int RollNo;
public int Age;
}
Here, we declared a structure named Student with three data members: Name, RollNo, and Age.

5.6 Accessing Members of a Structure

We can access the members of a structure using the dot (.) operator. Example:

s1.Name = "Basant";
s1.RollNo = 12;
s1.Age = 18;
This assigns values to the data members of structure s1.

5.7 Passing a Structure to a Method

Structures can be passed to a method as a parameter by value or by reference. Example: Pass by Value

void ShowStudent(Student s)
{
Console.WriteLine("Name:" + s.Name);
Console.WriteLine("RollNo:" + s.RollNo);
}
We can also pass a structure by reference using the ref keyword.

5.9 Arrays of Structures

We can create an array of structures to store multiple records of the same type. Example:

Student[] students = new Student[5];
This creates an array of 5 Student structures. We can also initialize the array using an initializer list.
Student[] Students =
{
new Student { Name = "Ram", RollNo = 1, Age = 17 },
new Student { Name = "Shyam", RollNo = 2, Age = 18 }
};

error: Content is protected !!
Scroll to Top