Structs → Classes
Data types and object-oriented programming
Introduction
In this lesson, you'll learn about structs → classes in C#. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C, you're familiar with data types and object-oriented programming.
C# has its own approach to data types and object-oriented programming, which we'll explore step by step.
The C# Way
Let's see how C# handles this concept. Here's a typical example:
// Class — reference type, heap-allocated
public class Person {
public string Name { get; set; }
public int Age { get; private set; }
public Person(string name, int age) {
Name = name;
Age = age;
}
public string Greet() => "Hi, I'm " + Name;
public void Birthday() => Age++;
}
// Struct — value type, stack-allocated (like C struct)
public record Point(double X, double Y);
var p = new Person("Alice", 30);
Console.WriteLine(p.Greet());
p.Birthday();
Console.WriteLine(p.Age);Comparing to C
Here's how you might have written similar code in C:
#include <stdio.h>
#include <string.h>
typedef struct {
char name[50];
int age;
} Person;
void person_greet(const Person *p) {
printf("Hi, I'm %s\n", p->name);
}
void person_birthday(Person *p) {
p->age++;
}
int main() {
Person p;
strncpy(p.name, "Alice", sizeof(p.name));
p.age = 30;
person_greet(&p);
person_birthday(&p);
}You may be used to different syntax or behavior.
C# class is a reference type (heap); C# struct is a value type (like C struct)
You may be used to different syntax or behavior.
C# classes bundle data and methods; C separates them
You may be used to different syntax or behavior.
C# properties (get/set) replace C's manual getter/setter functions
You may be used to different syntax or behavior.
C# record provides immutable value types with auto-equality
Step-by-Step Breakdown
1. Class = Data + Methods
A C# class combines the C struct (data) and associated functions (methods) into one unit with access control.
typedef struct { char name[50]; int age; } Person;
void person_greet(const Person *p);public class Person {
public string Name { get; }
public string Greet() => "Hi, I'm " + Name;
}2. Properties
C# properties look like fields but can have custom get/set logic, replacing C's convention of getter/setter functions.
public int Age { get; private set; } // read publicly, write internally only3. record for Value Types
C# records are immutable by default with auto-generated equality — perfect for data transfer objects replacing C structs.
record Point(double X, double Y);
var p = new Point(1.0, 2.0);
var q = p with { Y = 3.0 }; // new copy with different YCommon Mistakes
When coming from C, developers often make these mistakes:
- C# class is a reference type (heap); C# struct is a value type (like C struct)
- C# classes bundle data and methods; C separates them
- C# properties (get/set) replace C's manual getter/setter functions
Key Takeaways
- C# class replaces C struct + associated functions
- Properties replace manual getter/setter functions
- record provides immutable value types
- C# has inheritance and polymorphism; C uses function pointers