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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

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 …