C
C#

C to C#

10 lessons

Progress0%
1Variables & Types2Functions & Methods3Arrays & Lists4Structs → Classes5Memory Management6Strings7File I/O8Object-Oriented Programming9Generics and Collections10Async and Concurrency
All Mirror Courses
C
C#
Structs → Classes
MirrorLesson 4 of 10
Lesson 4

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.

Mirror Card
C
From C:

In C, you're familiar with data types and object-oriented programming.

C#
In C#:

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:

C#
C# 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:

C
C (What you know)
#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);
}
Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

C# class is a reference type (heap); C# struct is a value type (like C struct)

Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

C# classes bundle data and methods; C separates them

Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

C# properties (get/set) replace C's manual getter/setter functions

Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

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.

C
C
typedef struct { char name[50]; int age; } Person;
void person_greet(const Person *p);
C#
C#
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.

C#
C#
public int Age { get; private set; } // read publicly, write internally only

3. record for Value Types

C# records are immutable by default with auto-generated equality — perfect for data transfer objects replacing C structs.

C#
C#
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 Y

Common 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
Common Pitfall
Don't assume C# works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in C# to practice these concepts.
PreviousNext