Skip to content
dreamcode
dreamcode
Map
Classes & objects
Lesson 8 of 21
+15 XP on finish
C# OOPChapter 3 · C# Object Oriented Programming

Objects and classes

C# is an object-oriented language. A class is a blueprint, and an object is an instance of that class. Use the new keyword to instantiate an object.

Worked example
class Cloud {
    public string Shape;
    public int Altitude;

    public Cloud(string shape, int altitude) {
        Shape = shape;
        Altitude = altitude;
    }
}

// In Main:
Cloud myCloud = new Cloud("cumulus", 3000);

How it reads

  • public string Shape declares a public field
  • public Cloud(...) is a constructor used to initialize the object
  • new Cloud(...) creates a new instance on the heap
Cloud tip: Fields are marked with access modifiers like public or private. private fields can only be accessed within the class itself.

Check your understanding

0 / 2

Answer all 2 to complete this lesson and earn 15 XP.

  1. 1. What keyword is used to instantiate a class in C#?
  2. 2. What is the primary purpose of a constructor?
Answer every question to unlock the next lesson.