16. Structs in C#
Level: Beginner
- What a struct is
- Basic difference between struct and class
- Create a simple struct
- Understand value copy behavior
- Know when beginners should use classes instead
- Use a struct for small data
A struct is a small data type you create.
It can hold multiple values together.
For beginners, remember this simple rule:
Use class for normal objects.
Use struct only for small simple values.
The Problem: Some Data is Small and Simple
Example: marks in three subjects.
English: 80
Maths: 90
Science: 85
This can be grouped into one small struct.
Quick Definitions
- Struct - Small custom data type
- Class - Main way to model real objects
- Value type - Assignment copies the value
- Reference type - Assignment copies the reference to the object
- Copy - Separate duplicate value
Step 1: Create a Struct
public struct Marks
{
public int English;
public int Maths;
public int Science;
}
Step 2: Use the Struct
Marks marks;
marks.English = 80;
marks.Maths = 90;
marks.Science = 85;
Console.WriteLine(marks.Maths);
Output:
90
Struct Copy Behavior
Struct values are copied.
Marks marks1;
marks1.English = 80;
marks1.Maths = 90;
marks1.Science = 85;
Marks marks2 = marks1;
marks2.Maths = 100;
Console.WriteLine(marks1.Maths);
Console.WriteLine(marks2.Maths);
Output:
90
100
Changing marks2 does not change marks1.
Struct vs Class
| Question | Class | Struct |
|---|---|---|
| Best for | Real objects | Small values |
| Example | Student, Teacher | Marks, Point |
| Assignment | Copies reference | Copies value |
| Beginner default | Yes | No |
Full Example: Marks Struct
Expected output:
English: 80
Maths: 90
Science: 85
Total: 255
When You'll Use This in SMS
Most School Management System objects should be classes:
- Student
- Teacher
- FeeAccount
- AttendanceRecord
Structs are useful for small values:
- Marks summary
- Date range
- Simple point or coordinate
Beginner rule:
If it represents a real person or record, use class.
If it is a tiny value, struct may be okay.
Try This Now
Run the full example. Then experiment:
- Add
Socialmarks - Update
GetTotal() - Copy
marksto another variable - Change the copy and check the original
C# structs video coming soon on NexCoding YouTube. Subscribe for updates.
Common Mistakes
Mistake 1: Using struct for everything
Use classes for most beginner projects.
Mistake 2: Forgetting structs are copied
Changing a copied struct does not change the original.
Mistake 3: Making very large structs
Structs should be small.
Practice Task
Create a struct named FeeSummary with:
TotalFeesPaidAmountGetBalance()
Print the balance.
Quick Revision
| Question | Answer |
|---|---|
| What is a struct? | Small custom data type |
| Beginner default? | Class |
| Struct assignment copies what? | Value |
| Good struct example? | Marks |
| Good class example? | Student |
A struct is a small custom value type.
Beginners should use classes more often. Use structs only for small simple values.
The value is copied. Changing the copy does not change the original.
Use ChatGPT, Claude, or Copilot to go deeper on C# structs. Try these prompts:
"Explain struct vs class like I am a beginner""Give me 5 struct practice tasks""Explain value copy behavior with simple examples""Quiz me with 5 beginner questions about structs"
💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.