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 the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …