C# Class and Object with examples

Introduction

C# is an object-oriented programming language, which means it works with objects and classes. A class is a blueprint or a template for creating objects that have similar properties and behaviors. An object is an instance of a class, which can interact with other objects, classes, and the program as a whole.

What is a Class?

A class is a user-defined data type in C# that groups related data and functions together. It provides a blueprint for creating objects, which means that it defines the properties and behaviors that an object of that class should have. A class can contain fields, properties, methods, and events that define its behavior.

Creating a Class

To create a class in C#, you use the class keyword, followed by the name of the class. Here's an example:

1public class Person 2{ 3 public string Name; 4 public int Age; 5}

This creates a Person class with two fields, Name and Age.

What is an Object?

An object is an instance of a class. It's a concrete entity that has its own set of properties and behaviors, which are defined by the class. You can create multiple objects from the same class, and each object has its own unique state and behavior.

Creating Objects from a Class

To create an object in C#, you use the new keyword, followed by the name of the class. Here's an example:

1Person person = new Person();

This creates a Person object called person.

Accessing Class Members using Object

To access the members of a class, such as its fields and methods, you use the dot notation with the object name. Here's an example:

1person.Name = "Max"; 2person.Age = 24;

This sets the Name and Age fields of the person object.

Creating Multiple Objects of a Class

You can create multiple objects from the same class, each with its own state and behavior. Here's an example:

1Person person1 = new Person(); 2person1.Name = "Max"; 3person1.Age = 24; 4 5Person person2 = new Person(); 6person2.Name = "Bob"; 7person2.Age = 25;

This creates two Person objects, person1 and person2, with different states.

Creating Objects in a Different Class

You can create objects of one class in another class. Here's an example:

1public class Program 2{ 3 static void Main(string[] args) 4 { 5 Person person = new Person(); 6 person.Name = "Max"; 7 person.Age = 24; 8 9 Console.WriteLine("Name: " + person.Name); 10 Console.WriteLine("Age: " + person.Age); 11 } 12}

This creates a Person object in the Program class, sets its Name and Age fields, and then displays them on the console.

Why Objects and Classes?

Objects and classes are essential concepts in object-oriented programming, as they allow you to organize your code into reusable and manageable structures. By defining classes and creating objects, you can encapsulate data and behavior, make your code more modular, and reduce complexity.

C# provides a powerful object-oriented programming model that makes it easy to create and use classes and objects. Whether you're building a small console application or a large-scale enterprise system, understanding objects and classes is essential to writing effective and maintainable code.