C# Object and Class2 min read

C# is an object-oriented programming language and classes and objects are the fundamental components of OOP’s. Everything in C# is built upon classes and objects. Let us learn classes and objects with an example.

C# Class

A class is a user-defined blueprint or prototype for creating Objects. We can say that it is a template for creating objects for a class.

A class can have fields, methods, constructors, etc and these members can be accessed and used by creating an instance of that class that is an object.

Example for defining a class with few fields and methods in C#.

The definition of the class starts with the keyword class followed by a class name. The body of the class is enclosed within the curly brace. All the members of the class are declared within the body of the class as shown above.


C# Object

Objects are real-world entities. Example bike, Lamp, pen, table, etc. It is an instance of a class that has states and behavior. It can be physical and logical.

The Class members are accessed by creating an object of that class and is a runtime entity created at runtime.

To create an object of a class we use the keyword new.

In the above code, Rectangle is a type and rect is a reference variable that refers to the instance of the Rectangle class. And the new keyword allocates the memory at runtime in a program.


Example1: C# Class and Object

Let see an example where we create a class Rectangle and have a method calculate the area and also display the area value.

Output:

Area: 6


Example1: C# Class and Object

In the example below, we will put the main method at the different classes and create multiple objects for the Student class. Also, display the student information.

Output:

Roll: 1, Name: John Mac
Roll: 2, Name: Ryan Blanc

Also, a constructor is included in order to initialize the class field.


MORE

Find a pair with the Given Sum in an Array in Java

In this tutorial, we will write a java program to find the sum pair in an array. Before that, you may go through the following …
Read More

C++ Program to display Pascal Triangle

In this tutorial, we will write a C++ program to display pascal’s triangle. Before that, you may go through the following topic in C++. for …
Read More

C++ Program to Calculate Sum of Geometric Progression

In this tutorial, we will learn and write a program to find the sum of GP series in C++. Before that, you may go through …
Read More

C++ Program to find the Transpose of a Matrix

In this tutorial, we will learn and write a program to find the transpose of matric in C++. Before that, you may go through the …
Read More

How to Split a String in C++

Let us learn how to split string in C++ programming language. We will look at a few ways to split a string. The splitting of …
Read More

C++ Program to calculate Average of Numbers using Arrays

In this tutorial, we will learn and write a C++ program to find the average of numbers using an array. To understand the coding, you …
Read More