Classes
In this page:
Classes
A class is a blueprint that bundles data (fields) and behavior (methods) into a single type. You define it once and create as many independent objects from it as you need. Classes are the foundation of object-oriented programming in C#.
Note: Class names conventionally use PascalCase, like BankAccount.
Example: Classes
using System;
class Dog
{
public string Name;
public int Age;
public void Bark()
{
Console.WriteLine($"{Name} says Woof!");
}
}
class Program
{
static void Main(string[] args)
{
Dog d = new Dog();
d.Name = "Rex";
d.Age = 3;
d.Bark();
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: