Member-only story

C# Classes and Objects with Examples

Shahzad Aslam
3 min readApr 21, 2024

--

A class is basic pillar of OOP that is called when instance of a class is created. A class can have fields, properties, method, constructor and destructor etc. When we create an instance of class. This instance of class is used to access of fields, methods, properties, constructor (constructor can zero argument or multiple argument) and destructor. A class is reference type and stores in heap.

C# Classes and Objects with Examples

Declaration and Initialization:

Data Member Declaration:

You declare an attribute and variable of class in such way.

Example

// Fields (attributes or variables)
private int FruitPrice;
private string FruitName;
private bool FruitIsInMarket;

Data Member Initialization:

You can initialize an attribute and variable of class in such way.

Example

// Initializing data member of class
FruitPrice = 100;
FruitName = Mango;
FruitIsInMarket = Yes;

Constructor In Class:

Constructor is special method that called when object/instance of class is created

Example

// Constructor
public Fruit(int price, string name, bool…

--

--

Shahzad Aslam
Shahzad Aslam

Written by Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.

No responses yet