Complete C# Tutorial

C# Structure Example: A Simple Way to Organize Data

A Structure (or struct) is a value type in C# that helps you store related information in one place.

Instead of creating multiple variables, you can define a struct to hold everything together.

Have you ever filled out a form at a doctor’s office? 🏥 They usually ask for:

  • Your Name
  • Your Age
  • Your Weight
  • Your Height

All these pieces of information belong together. You wouldn’t store them separately in different places, right? That would be messy!

That’s exactly why Structures (structs) exist in C#! They group related data together in a clean and organized way.

 


Why Use Structures in C#?

Groups related data – Keeps everything neat and organized.
Faster than classes – Structs are stored in the stack, so they are quick to access.
No need for a constructor – You can use a struct without new.
Useful for small data models – Perfect for storing simple data like points, colors, or measurements.

Real-World Example: Storing Employee Data

Let’s say you’re building an employee management system. You need to store an employee’s name, age, and salary.

Without a struct, you’d need separate variables for each employee—which is messy. Instead, let’s use a Structure C# Example to make it cleaner.

C# Structure Example: Employee Record

Step 1: Define a Structure

First, let’s create a struct to store employee details

				
					using System;

struct Employee
{
    public string Name;
    public int Age;
    public double Salary;
}

class Program
{
    static void Main()
    {
        Employee emp1;
        emp1.Name = "Steven";
        emp1.Age = 30;
        emp1.Salary = 50000;

        Console.WriteLine("Employee Details:");
        Console.WriteLine("Name: " + emp1.Name);
        Console.WriteLine("Age: " + emp1.Age);
        Console.WriteLine("Salary: $" + emp1.Salary);
    }
}
				
			

Output:

				
					Employee Details:  
Name: Steven  
Age: 30  
Salary: $50000  
				
			

Explanation:

1️⃣ We defined a struct called Employee with three fields: Name, Age, and Salary.
2️⃣ Inside Main(), we declared a variable emp1 of type Employee.
3️⃣ We assigned values to emp1 without using new because structs don’t require a constructor.
4️⃣ Finally, we printed the employee details.

Using a Struct with a Constructor

Want a cleaner way to initialize a struct? Use a constructor! 🚀

				
					using System;

struct Employee
{
    public string Name;
    public int Age;
    public double Salary;

    public Employee(string name, int age, double salary)
    {
        Name = name;
        Age = age;
        Salary = salary;
    }

    public void Display()
    {
        Console.WriteLine($"Name: {Name}, Age: {Age}, Salary: ${Salary}");
    }
}

class Program
{
    static void Main()
    {
        Employee emp2 = new Employee("Emily", 25, 60000);
        emp2.Display();
    }
}
				
			

Output:

				
					Name: Emily, Age: 25, Salary: $60000  
				
			

What’s Different Here?

✅ We added a constructor to initialize Name, Age, and Salary in one step.
✅ We added a Display() method inside the struct to make printing easier.
✅ The struct is now more reusable and cleaner.

When to Use Structs vs. Classes?

Feature Struct (struct) Class (class)
Memory Type Stored in stack Stored in heap
Performance Faster for small data Better for complex data
Data Size Best for small objects Best for large objects
Inheritance ❌ No inheritance ✅ Supports inheritance
Constructor Not required Usually needed

💡 Use structs when you need simple, lightweight data storage like points, colors, or small records.
💡 Use classes when you need complex behaviors, inheritance, or large objects.

Conclusion

Structures (structs) in C# help you organize related data in a simple and efficient way. They are lightweight, fast, and perfect for small objects that don’t need inheritance.

Now, go ahead and try using structs in your own projects! And hey, if you have difficulty or a question, drop a comment. We will be happy to help you. 😊

Next What?

In the next lesson, you’ll learn about the Scoped Statement in C#—a cool way to control variable lifetimes! Stay tuned! 

Leave a Comment

Share this Doc

Structure

Or copy link