Classes
A class is a blueprint, like the plan for a house or the design of a toy. It describes what things of that type know and can do.
In this page:
Syntax
class ClassName
{
public type Field;
public void MethodName()
{
// uses Field
}
}
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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Confusing a class (the blueprint) with an object (an instance).
- Forgetting to mark members
publicand then being unable to reach them from other classes, since members are private by default. - Putting too many unrelated responsibilities into one class.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: