Skip to main content

How to Declare a Class in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A class describes a kind of thing your program works with: what data each one has, and what it can do. In C#, you declare the class once, then create as many objects of it as you need, each with its own data.

Syntax​

Syntax
class <ClassName>
{
public <Type> <PropertyName> { get; set; }

public <ReturnType> <MethodName>() => <expression>;
}

<ClassName> <variable> = new();
PartWhat it does
class <ClassName> { ... }Declares the class. Everything between the braces belongs to it
public <Type> <PropertyName> { get; set; }A property: a piece of data each object has
public <ReturnType> <MethodName>()A method: something each object can do, using its own data
new()Creates an object of the class

Microsoft's classes guide: "A class defines a type that groups data and behavior." And: "The class declaration defines the type; it doesn't create an object. The new operator creates an instance".

The Example​

A Book class has a title, which starts as Untitled, a number of pages, and a Describe method that puts the two in a sentence. The program creates two books, then a second variable for the first book.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which creates a Book with the title Dune and 412 pages and prints Describe, creates another Book and prints Describe, then sets a variable same to the first book, changes its title to Dune Messiah and prints the first book's Describe, with a Book class that has Title, Pages and Describe.

main.cs
Book book = new();
book.Title = "Dune";
book.Pages = 412;
Console.WriteLine(book.Describe());

Book another = new();
Console.WriteLine(another.Describe());

Book same = book;
same.Title = "Dune Messiah";
Console.WriteLine(book.Describe());

class Book
{
public string Title { get; set; } = "Untitled";
public int Pages { get; set; }

public string Describe() => $"{Title}, {Pages} pages";
}

Step 2: Run It​

Terminal
dotnet run main.cs
Output
Dune, 412 pages
Untitled, 0 pages
Dune Messiah, 412 pages

A zsh terminal where dotnet run main.cs prints Dune, 412 pages, then Untitled, 0 pages, then Dune Messiah, 412 pages.

  1. The first book got its title and pages, and Describe used them.
  2. The second book is a separate object with its own data: the starting title and 0 pages.
  3. Book same = book; didn't copy the book. Both variables refer to the same object, so changing the title through same changed book too.

That third result is what "reference type" means. The guide: "Classes are reference types: a variable holds a reference to an object rather than the object's data."

The command is the same in zsh, bash and PowerShell.

A Variable Needs an Object​

Declaring a variable of a class type doesn't create an object. Without new, there's nothing to set a title on:

main.cs
Book book;
book.Title = "Dune";

class Book
{
public string Title { get; set; } = "Untitled";
}
Output
/Users/you/demo/main.cs(2,1): error CS0165: Use of unassigned local variable 'book'

The build failed. Fix the build errors and run again.

Give the variable an object first: Book book = new();.

Common Mistakes​

MistakeWhat happensDo this instead
Declaring Book book; and using iterror CS0165: Use of unassigned local variable 'book'Book book = new();
Expecting Book same = book; to make a copyBoth variables change the same objectCreate a new object and copy the values you need

Troubleshooting​

SymptomLikely causeFix
CS0165 on a class variableIt was declared but never given an objectAssign new()
Changing one object changes "another"Both variables refer to the same objectCreate a separate object with new()

FAQ​

What's the difference between a class and an object?​

The class is the description, written once: a book has a title and pages. Each object is one book, created with new, with its own title and pages.

Where do I put the class in a one-file program?​

After the top-level code, as in Step 1. In a bigger project, each class usually gets its own file.

Conclusion​

Declare a class with class Name { ... }, give it properties for its data and methods for what it does, and create objects of it with new(). Classes are reference types: two variables can refer to one object, and a variable without an object is error CS0165.

Additional Resources​