Struct Declaration and Usage in C#
Last Updated :
16 Oct, 2025
In C#, a struct (structure) is a value type used to represent data that logically belongs together.Structs are value types, meaning their data is stored inline. They may reside on the stack or heap depending on the variable’s context, making it more efficient for small, lightweight objects. Structs are best suited for representing simple entities like coordinates, points, or colors.
Struct Declaration
A struct in C# is declared using the struct keyword. It can contain fields, properties, methods, constructors and indexers, but cannot inherit from another class or struct.
Syntax
struct StructName
{
// Fields
// Properties
// Constructors
// Methods
}
Example:
C#
struct Point
{
public int X;
public int Y;
// Parameterized constructor
public Point(int x, int y)
{
X = x;
Y = y;
}
// Method
public void Display()
{
Console.WriteLine($"X: {X}, Y: {Y}");
}
}
Usage:
C#
class Program
{
static void Main()
{
Point p1 = new Point(10, 20);
p1.Display();
}
}
Output:
X: 10, Y: 20
Accessing Struct Members
Struct members are accessed using the dot (.) operator, just like class members.
C#
struct Rectangle
{
public int Length;
public int Breadth;
public int Area()
{
return Length * Breadth;
}
}
class Program
{
static void Main()
{
Rectangle rect = new Rectangle();
rect.Length = 5;
rect.Breadth = 10;
Console.WriteLine("Area: " + rect.Area());
}
}
Output:
Area: 50
Default Constructor and Initialization
Every struct automatically gets a default constructor that initializes all fields to their default values (numeric fields → 0, reference fields → null).
C#
struct Student
{
public int Id;
public string Name;
}
class Program
{
static void Main()
{
Student s1 = new Student(); // uses default constructor
Console.WriteLine($"Id: {s1.Id}, Name: {s1.Name}");
}
}
Output:
Id: 0, Name:
Passing Structs as Parameters
Since structs are value types, they are copied by value when passed to a method. Changes inside the method do not affect the original instance unless passed using the ref or out keyword.
C#
struct Counter
{
public int Value;
}
class Program
{
static void Modify(Counter c)
{
c.Value += 10;
}
static void Main()
{
Counter c1 = new Counter();
c1.Value = 5;
Modify(c1);
Console.WriteLine(c1.Value); // Output: 5 (unchanged)
}
}
Structs and Interfaces
Structs can implement interfaces to provide specific behavior while still being value types.
C#
interface IDisplay
{
void Show();
}
struct Point : IDisplay
{
public int X;
public int Y;
public void Show()
{
Console.WriteLine($"({X}, {Y})");
}
}
When to Use Structs
- The object represents a single, small piece of data (like a coordinate, color, or size).
- You want value semantics instead of reference semantics.
- The data is immutable and does not require complex inheritance or polymorphism.
Avoid using structs when
- The data is large or mutable.
- You need to use inheritance.
- You require a default parameterless constructor with custom logic.
Which of the following statements about structs in C# is TRUE?
-
Structs are reference types
-
Structs can inherit from other structs
-
Structs are value types and copied by value
-
Structs must always be created using the new keyword
Explanation:
Structs in C# are value types, meaning when they are assigned or passed to methods, a copy of the data is made. They cannot inherit from other classes or structs (except System.ValueType implicitly).
What happens when a struct is passed to a method without using ref or out?
-
The original struct instance is modified
-
A reference to the struct is passed
-
A copy of the struct is passed
-
Which of the following is NOT allowed in a C# struct?
-
-
-
Inheriting from another class or struct
-
Declaring fields and properties
Explanation:
Structs cannot inherit from another class or struct. However, they can implement interfaces, define methods, fields, properties, constructors and indexers.
Quiz Completed Successfully
Your Score : 2/3
Accuracy : 0%
Login to View Explanation
1/3
1/3
< Previous
Next >
Explore
Introduction
Fundamentals
Control Statements
OOP Concepts
Methods
Arrays
ArrayList
String
Tuple
Indexers