A Structure (struct) in C# is a value type data type.
It helps you to create a single variable that can hold related data of different data types.
The struct keyword is used to define a structure.
It is similar to a class, because both are user-defined
data types that can store multiple related variables of different data
types.
However, unlike classes, structures are value types and are
stored in stack memory, not heap.
Defining a Structure
In C#, a structure is defined using
the struct keyword.
A structure can contain:
· Fields
· Constructors
· Constants
· Properties
· Indexers
· Methods
· Events
Syntax:
Access_Modifier struct StructureName{ // Fields // Parameterized constructor // Constants // Properties // Indexers // Events // Methods}
Example: Defining a Structure
struct Books{ public string title; public string author; public string subject; public int book_id;}
Here:
·
Books
is the name of the structure.
·
It contains four fields: title, author, subject,
and book_id.
Declaring Structure Variables
Before using a structure, we must create
a structure variable.
We use the structure name followed by a variable name to
declare it.
Example:
struct Books{ public int id;} // Declaring structure variableBooks eng;
Now, eng is a variable of type Books.
You can access and assign values to its members like this:
eng.id = 101;Console.WriteLine(eng.id);
Example: Structure with Data
using System; struct Books{ public string title; public string author; public int book_id; public void Display() { Console.WriteLine("Title: " + title); Console.WriteLine("Author: " + author); Console.WriteLine("Book ID: " + book_id); }} class Program{ static void Main() { Books book1; book1.title = "C# Programming"; book1.author = "John Doe"; book1.book_id = 101; book1.Display(); }}
Output:
Title: C# ProgrammingAuthor: John DoeBook ID: 101
Features of C# Structures
The main features of C# structures are:
1. Structures can contain methods, fields, indexers, properties, operator methods, and events.
2. Structures can have constructors (but not destructors). A default constructor is automatically provided and cannot be defined manually.
3. Structures cannot inherit from other structures or classes.
4. Structures cannot be used as a base for other structures or classes.
5. A structure can implement one or more interfaces.
6. Structure
members cannot be declared as abstract, virtual,
or protected.
7. When
you create a struct object using the new
keyword, the constructor is called and memory is allocated.
8. Structs
can also be instantiated without using new, but then all fields must be
manually initialized before use.
9. Structures are stored in stack memory, making them faster and lightweight compared to classes.
using System;
struct Student
{
public int id;
public string name;
}
class Program
{
static void Main()
{
// Using new keyword
Student s1 = new Student(); // Calls default constructor internally
Console.WriteLine("Using new keyword:");
Console.WriteLine("ID: " + s1.id); // Default value → 0
Console.WriteLine("Name: " + s1.name); // Default value → null
Console.WriteLine();
// Without using new keyword
Student s2; // Declaration only (not initialized)
s2.id = 10; // Must initialize manually
s2.name = "Sita";
Console.WriteLine("Without using new keyword:");
Console.WriteLine("ID: " + s2.id);
Console.WriteLine("Name: " + s2.name);
}
}
Comments
Post a Comment