Introduction
You might have heard several times that C# is an object-oriented programming language. One of the important words here is "object."In C#, an Object is a real-world entity. Objects are always associated with a class. A Class is like an object constructor or a "blueprint" for creating objects. Class is a group of similar objects. It can have fields, methods, constructors, etc.
Students are generally confused about what classes and objects are. Once you start learning C# and observe the code, you will be able to understand both terms in detail.
Consider banking software. Some of the attributes or states that each bank will have is:
- Name
- IFSC code
- Phone
- Address
There can be many other attributes also but let us restrict ourselves to the above attributes only. When we say that each bank will have the above attributes, we mean that each bank will have the above properties regardless of its size of capital.
Apart from the above attributes, each bank will also have certain functions or behaviours like:
- Giving loan
- Opening a new bank account
- Depositing money
- Providing money withdrawal facility
Again there can be numerous other functions too that a bank can perform. But let us consider only the above attributes and functions for the time being.
This collection of attributes (or state) and functions( or behaviours) is known as a class. We can clearly see that here, "bank" is a class that has a prototype. Each bank like SBI, HDFC, AXIS must have similar features (attributes and behaviours).
Here SBI, HDFC, AXIS are objects. They have a copy of the original blueprint. But they are independent of each other.
Even now if you are confused with classes and objects, then it is completely normal. We shall see the code implementation and examples which will make the concept crystal clear.
Recommended Topic, Palindrome in C# and Ienumerable vs Iqueryable.
C# Classes
A Class is like an object constructor or a "blueprint" for creating objects. Class is a group of similar objects. It can have fields, methods, constructors, etc. A class in C# is created by using the keyword "class."
Creating a Class
Syntax:
<Modifier> class className{
// data members
// functions
}
Example:
using System;
class CodingNinjas{
//data member (attribute)
string course1 ="C#";
// methods or functions (behaviour)
public static void Main(){
Console.WriteLine(“DSA”);
}
}
Output:
DSA
Explanation:
- We created a class named CodingNInjas using the class keyword.
- public represents the modifier for the class.
- A class contains data members (often called variables) and methods( or functions).
Components of a Class
The various components of a class are:
- Identifier: Identifier is simply the name of the class. In the above example, the name of the class is CodingNinjas. By convention, the first character of the name of the class is in capital letters.
- Modifier: Modifiers are used to specify the scope of accessibility of the class. The various types of modifiers are public, private, protected, internal, protected internal, private protected.
- Keyword class: To create a class the keyword "class" is used. For example, class CodingNinjas.
- Body: The body of the class contains all the attributes and behaviours of that class. The class is surrounded by curly braces {}.