Class & Objects in C#

Rumman Ansari   Software Engineer   2023-03-30   5780 Share
☰ Table of Contents

Table of Content:


Objective

• Introduction to Classes
• Data-Types
• Access Level Modifiers
• Object State - Properties
• Constructors
• OOP Concepts – Encapsulation
• Main Method
• Sample Programs

Introduction to Classes

We know Class describes the state & behaviour of objects. Thus a Class decides/ defines what an object does with what the data it holds. After creating/designing a Class, any number of similar objects can be created.

Thus, a Class becomes the Template for an Object & an Object becomes the instance of its Class.

How to write a C# Class

he syntax of the class is as follows:


class ClassName
{
//code here
}


For e.g.; Suppose we have to create a class for Customer, then


 
Class Customer
{
//code here;
}

In C#, class is a group of similar objects. It is a template from which objects are created. It can have fields, methods, constructors etc.

Let's see an example of C# class that has two fields only.


 public class Customer
 {  
     int id;//field or data member   
     String name;//field or data member  
 }  

Classes – Properties, Get & Set

The variables declared inside a Class are by default not accessible from other Classes, but to access/ assign the values from outside, the Properties are used.

The properties are special methods called as Accessors, which allows the data to be assigned to/ accessed from the variable, through Read-only/ Write- only/ Read-Write permissions. The properties enable a class to expose only the getting & setting of values, while hiding the implementation or verification code.

The syntax of the property is as follows:


 
Accessmodifier datatype PropertyName
{
get { return variablename; }
set { variablename= value; }
}


The property contains 2 blocks, a get block, which is called Get Accessor & set block, which is called Set Accessor. When the data has to be read using the property, the get accessor will work & when the data has to be assigned to, the set accessor will work.

Thus the property restricts the access to the value. Property containing only the get block/ get accessor is a Read-Only Property. A property containing only the set block/ set accessor is the Write-Only property, the Property containing both the Get & Set Accessor is a Read-Write Property.

For e.g.; we will include the properties for Id & name in our customer class. The Id of the customer is restricted from modification whereas the name can be given with Read-Write Permission. Our Class will look like as follows:


 
class Customer
{
//Declaration of Variable
private int _id = 0;
//Property for ID restricted from assigning new values
public int ID
{
get { return _id; }
}
//Declare Variable for name & use Property for Read & Write
private string _name = "abc";
public string Name
{
get { return _name; }
set { _name = value; }
}
}


Object

In C#, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc. In other words, object is an entity that has state and behavior. Here, state means data and behavior means functionality.

Object is a runtime entity, it is created at runtime. Object is an instance of a class. All the members of the class can be accessed through object.

Let's see an example to create object using new keyword.


 Student s1 = new Student();//creating an object of Student    

In this example, Student is the type and s1 is the reference variable that refers to the instance of Student class. The new keyword allocates memory at runtime.