C# Classes and Objects - Complete Guide
Introduction to Classes and Objects
In C#, a class is a template that defines the structure and behavior of objects. It can contain fields, properties, methods, and constructors. An object is a runtime instance of a class, created using the 'new' keyword.
Classes are a cornerstone of object-oriented programming (OOP) in C#. They support encapsulation, inheritance, and polymorphism, helping organize code into reusable and maintainable units.
Class Declaration and Object Creation
You declare a class with the 'class' keyword and create objects from it using 'new'. Each object has its own copy of the class's fields and properties, but methods are shared definitions.
using System;
// Class declaration
public class Car
{
// Fields
public string Make;
public string Model;
public int Year;
// Method
public void DisplayInfo()
{
Console.WriteLine($"Make: {Make}, Model: {Model}, Year: {Year}");
}
}
class Program
{
static void Main(string[] args)
{
Car car1 = new Car();
car1.Make = "Toyota";
car1.Model = "Camry";
car1.Year = 2022;
Car car2 = new Car();
car2.Make = "Honda";
car2.Model = "Civic";
car2.Year = 2021;
Console.WriteLine("Car 1 Information:");
car1.DisplayInfo();
Console.WriteLine("\nCar 2 Information:");
car2.DisplayInfo();
// Objects are reference types
Car car3 = car1;
car3.Year = 2023;
Console.WriteLine("\nAfter modifying car3:");
Console.WriteLine("Car 1 year: " + car1.Year);
Console.WriteLine("Car 3 year: " + car3.Year);
}
}
Car 1 Information: Make: Toyota, Model: Camry, Year: 2022 Car 2 Information: Make: Honda, Model: Civic, Year: 2021 After modifying car3: Car 1 year: 2023 Car 3 year: 2023
Class Members and Access Modifiers
Classes can define members such as fields, properties, methods, and constructors. Access modifiers like public and private control visibility.
Public members are accessible from outside the class, while private members are hidden and only accessible within the class itself.
using System;
public class Person
{
private string name;
private int age;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public void Introduce()
{
Console.WriteLine($"Hello, my name is {name} and I'm {age} years old.");
}
private bool IsAdult()
{
return age >= 18;
}
public void CheckAdulthood()
{
if (IsAdult())
Console.WriteLine("I am an adult.");
else
Console.WriteLine("I am not an adult yet.");
}
}
class Program
{
static void Main(string[] args)
{
Person person = new Person("Alice", 25);
person.Introduce();
person.CheckAdulthood();
person.Name = "Bob";
person.Age = 16;
Console.WriteLine("\nAfter changes:");
person.Introduce();
person.CheckAdulthood();
// person.IsAdult(); // Error: inaccessible due to private modifier
}
}
Hello, my name is Alice and I'm 25 years old. I am an adult. After changes: Hello, my name is Bob and I'm 16 years old. I am not an adult yet.