Understanding classes and objects in C# is essential to master object-oriented programming (OOP). In C#, everything revolves around classes and objects, as they represent the blueprint and real-world entities in programming. This guide explores what classes and objects are, how to define them, create objects, use constructors, access modifiers, methods, fields, properties, and more. The concepts discussed here are crucial for writing clean, scalable, and maintainable C# applications.
A class in C# is a blueprint for creating objects. It encapsulates data for the object and methods to manipulate that data. It defines what properties and behaviors (methods) the objects of this class will have.
class ClassName
{
// Fields
// Properties
// Constructors
// Methods
}
class Car
{
public string model;
public int year;
public void Drive()
{
Console.WriteLine(model + " is driving.");
}
}
An object is an instance of a class. It holds actual values for the class members. Once a class is defined, we can create objects from it using the new keyword.
Car myCar = new Car();
myCar.model = "Toyota";
myCar.year = 2023;
myCar.Drive();
Fields are variables declared inside a class. They hold the state of an object.
public string name;
public int age;
Methods define behavior for objects. They are functions declared inside a class.
public void Speak()
{
Console.WriteLine("Hello!");
}
Properties provide access to fields with get and set accessors.
private int _price;
public int Price
{
get { return _price; }
set { _price = value; }
}
Constructors initialize object data when the object is created.
public Car(string model, int year)
{
this.model = model;
this.year = year;
}
class Person
{
public string name;
public int age;
public void Greet()
{
Console.WriteLine("Hello, my name is " + name);
}
}
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.name = "John";
p.age = 30;
p.Greet();
}
}
| Class | Object |
|---|---|
| Blueprint for creating objects | Instance of a class |
| Defines structure and behavior | Represents actual data |
| No memory is allocated | Memory is allocated when created |
| Keyword: class | Keyword: new |
You can initialize objects using initializer syntax without calling constructors explicitly.
var person = new Person { name = "David", age = 28 };
namespace MyApp.Models
{
public class Product
{
public string Name;
}
}
using MyApp.Models;
class Program
{
static void Main()
{
Product p = new Product();
p.Name = "Laptop";
Console.WriteLine(p.Name);
}
}
Classes and objects are the foundational pillars of object-oriented programming in C#. By defining classes, you set blueprints for the properties and behavior of objects. Through object instantiation, you bring these blueprints to life. This tutorial covered key topics such as fields, properties, methods, constructors, access modifiers, static members, and more. Mastering these concepts is essential for building robust, modular, and scalable C# applications.
Whether you are building desktop applications, web APIs, or enterprise systems, understanding how to structure and use classes and objects effectively will elevate your C# programming skills significantly.
Copyrights © 2024 letsupdateskills All rights reserved