capypad
0 day streak
csharp / beginner
Snippet

Introduction to Classes

A class is a blueprint for objects. In this example, the Book class defines what data a book has, and 'new Book()' creates an actual instance of it.

snippet.csharp
csharp
1
2
3
4
5
6
class Book {
public string title;
}
 
Book myBook = new Book();
myBook.title = "C# Basics";
Breakdown
1
class Book { ... }
Defines a new custom data type named Book.
2
public string title;
A public field that allows the object to store a title string.
3
Book myBook = new Book();
Creates a new object (instance) of the Book class in memory.