Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 9 / 68

Classes (Object-Oriented Programming)

In this lesson you will learn the basics of object-oriented programming with C# classes, so you can group data that shares the same characteristics. This is for anyone searching for "C# class how to use".

class defines a class, and a constructor (a method with the same name as the class) performs the initialisation. Think of it as an "animal blueprint" from which you can create as many concrete instances such as a dog or a cat as you like.

The sample code gives the Animal class two fields, Name and Sound, initialises them in the constructor, and builds a message in the Speak() method. Notice how new Animal("Dog", "Woof") creates an instance.

A common stumbling block for beginners is the difference between a class and an instance. A class is a blueprint; to actually use it you have to create an instance, and that idea takes some getting used to.

In real-world development, the larger the application, the more this object-oriented thinking matters, and much of the C# standard library is itself designed around classes.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)