C# Class and Object
To work with objects, we need to perform the following activities:
create a class
create objects from the class
Class is the blueprint for the object.
Create a class in C#
We use the class keyword to create an object. For example,
class ClassName {
Here, we have created a class named ClassName. A class can contain
fields - variables to store data
methods - functions to perform specific tasks
Let's see an example,
class Dog {
//field
string breed;
//method
public void bark() {
In the above example,
Dog - class name
breed - field
bark() - method
Note: In C#, fields and methods inside a class are called members of a class.
C# Objects
An object is an instance of a class. Suppose, we have a class called Dog.
Bulldog, German Shepherd, Pug are objects of the class.
Creating an Object of a class
In C#, here's how we create an object of the class.
ClassName obj = new ClassName();
Here, we have used the new keyword to create an object of the class. And, obj is the name of the
object. Now, let us create an object from the Dog class.
Dog bullDog = new Dog();
Now, the bullDog object can access the fields and methods of the Dog class.
Access Class Members using Object
We use the name of objects along with the “.” operator to access members of a class.
For example,
using System;
namespace ClassObject {
class Dog {
string breed;
public void bark() {
Console.WriteLine("Bark Bark !!");
static void Main(string[] args) {
// create Dog object
Dog bullDog = new Dog();
// access breed of the Dog
bullDog.breed = "Bull Dog";
Console.WriteLine(bullDog.breed);
// access method of the Dog
bullDog.bark();
Console.ReadLine();
}
}
}
Output:
Bull Dog
Bark Bark !!
In the above program, we have created an object named bullDog from the Dog class. Notice that
we have used the object name and the . (dot operator) to access the breed field and the bark()
method.
Creating Multiple Objects of a Class
We can create multiple objects from the same class. For example,
using System;
namespace ClassObject {
class Employee {
string department;
static void Main(string[] args) {
// create Employee object
Employee sheeran = new Employee();
// set department for sheeran
sheeran.department = "Development";
Console.WriteLine("Sheeran: " + sheeran.department);
// create second object of Employee
Employee taylor = new Employee();
// set department for taylor
taylor.department = "Content Writing";
Console.WriteLine("Taylor: " + taylor.department);
Console.ReadLine();
}
}
}
Output:
Sheeran: Development
Taylor: Content Writing
Creating objects in a different class
In C#, we can also create an object of a class in another class. For example,
namespace ClassObject {
class Employee {
public string name;
public void work(string work) {
Console.WriteLine("Work: " + work);
}
}
class EmployeeDrive {
static void Main(string[] args) {
// create Employee object
Employee e1= new Employee();
Console.WriteLine("Employee 1");
// set name of the Employee
e1.name="Gloria";
Console.WriteLine("Name: " + e1.name);
//call method of the Employee
e1.work("Coding");
Console.ReadLine();
}
}
}
Output
Employee 1
Name: Gloria
Work: Coding