Skip to main content

Classes in C#

Ayesha
EditReport

Classes in C#

In C#, a class is a blueprint for creating objects. It defines properties, methods, and events that represent the characteristics and behaviors of an object.

Video Explanationโ€‹


1. What is a Class?โ€‹

A class in C# defines the structure and behavior of objects. It includes fields, properties, methods, and constructors.

Example:โ€‹

public class Car {
public string color = "red";
public void Drive() {
Console.WriteLine("The car is driving.");
}
}

This example defines a class Car with a property color and a method Drive.


2. Defining a Classโ€‹

In C#, classes are defined using the class keyword.

Syntax:โ€‹

public class ClassName {
// fields, properties, methods, and constructors
}

Example:โ€‹

public class Person {
public string Name;
public int Age;
}

3. Fields in Classesโ€‹

Fields are variables that hold data related to an object. They are typically private but can be public as well.

Example:โ€‹

public class Person {
public string Name; // Field
}

4. Propertiesโ€‹

Properties provide controlled access to fields and use get and set accessors.

Example:โ€‹

public class Person {
private string name;

public string Name {
get { return name; }
set { name = value; }
}
}

5. Methods in Classesโ€‹

Methods define actions that an object can perform.

Example:โ€‹

public class Car {
public void Drive() {
Console.WriteLine("The car is driving.");
}
}

6. Constructorsโ€‹

Constructors initialize an object when it is created.

Example:โ€‹

public class Person {
public string Name;

public Person(string name) {
Name = name; // Constructor initializes the Name property
}
}

7. Access Modifiersโ€‹

Access modifiers control the visibility of class members (public, private, protected).

Example:โ€‹

public class Car {
private string color; // Private member
public void SetColor(string c) {
color = c; // Public method to set color
}
}

8. Static Classes and Membersโ€‹

Static classes cannot be instantiated, and static members belong to the class itself rather than an instance.

Example:โ€‹

public static class Utility {
public static void PrintMessage(string message) {
Console.WriteLine(message);
}
}

Summaryโ€‹

Classes are essential for object-oriented programming in C#. They provide a way to organize data and behaviors in your programs, making them more modular and maintainable.

Happy coding!

Track Your Progress

Done with this topic? Mark it as complete to track your progress.