Skip to main content

16. Structs in C#

Level: Beginner

ℹ️ What You'll Learn
  • 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

QuestionClassStruct
Best forReal objectsSmall values
ExampleStudent, TeacherMarks, Point
AssignmentCopies referenceCopies value
Beginner defaultYesNo

Full Example: Marks Struct

💻 Try It — Console App
💡 Paste into Program.cs and press F5⌥ GitHub
public struct Marks
{
public int English;
public int Maths;
public int Science;

public int GetTotal()
{
return English + Maths + Science;
}
}

Marks marks;
marks.English = 80;
marks.Maths = 90;
marks.Science = 85;

Console.WriteLine($"English: {marks.English}");
Console.WriteLine($"Maths: {marks.Maths}");
Console.WriteLine($"Science: {marks.Science}");
Console.WriteLine($"Total: {marks.GetTotal()}");

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:

  1. Add Social marks
  2. Update GetTotal()
  3. Copy marks to another variable
  4. Change the copy and check the original

ℹ️ Video Tutorial

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:

  • TotalFees
  • PaidAmount
  • GetBalance()

Print the balance.

Quick Revision

QuestionAnswer
What is a struct?Small custom data type
Beginner default?Class
Struct assignment copies what?Value
Good struct example?Marks
Good class example?Student

🎯 Q1: What is a struct in C#?

A struct is a small custom value type.

🎯 Q2: Should beginners use class or struct more often?

Beginners should use classes more often. Use structs only for small simple values.

🎯 Q3: What happens when a struct is copied?

The value is copied. Changing the copy does not change the original.


🤖Use AI to Learn Faster
⚠️ Important for beginners: Do NOT use AI to write your code yet. Type every example yourself. Your brain learns by doing, not by reading AI output. Use AI only to explain and quiz you — not to code for you. Once you have strong fundamentals, AI becomes a powerful productivity tool for repetitive tasks.

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.

Next Article

Collections - List and Dictionary ->

nexcoding.in