What is Classes and Object?
“
Since the Beginning of this C# tutorials, we have been using classes. And Now, we should have a sense of what a class is for and how to specify one. In this lesson, we will discuss more about the Classes and its Parts.
To understand the concept of Classes and Object, let't take the example of laptop. Let's suppose, we have an Laptop, this laptop have some Properties like Display, RAM, color, Operating System, Hard Disk etc. and this Laptop as some methods too, like Login, Shutdown, Processing, Play videos, etc. Now using this same laptop concept we can make more Laptops. each Laptop will have the same Methods but different Properties as shown in above figure.
So we can say that, first we make the sketch of Laptop, and just using the sketch we make more Laptops having same Methods but different properties. so its an easy example to understand about classes and objects. so let's move on in programming world to understand the classes and object using Laptop example:
Classes and Objects:
The Classes is the Sketch or Blueprint, same like Laptop Class with some properties and Methods. so using this Laptop class, we can create more Laptops known as Object having same methods but different properties. Classes are the building block of Object oriented Programming.
Defining a Class:
A Class definition starts with the keyword class followed by the class name and the class body enclosed by a Pair of curly braces. Following is the General form of a Class Definition:
Access-specifier class className
{
//class variables or Properties:
Access-specifier Data-Type variable1;
Access-specifier Data-Type variable2;
//class Methods or functions:
Access-specifier Return-Type method1(Parameter-List)
{
//method body:
}
}
“
» Default access modifiers at Namespace Level are Internal :
» Default access modifiers at Class level are Private :
Using a Class:
After Declaring the Class, we can use the class methods and Properties using the Dot Operator(.), The Dot Operator links the Name of an object with the name of a Member. In order to use the Class members, we need to create an Object of that class first. then using the object along with dot operator, we can access the members of Class. Let's have an example to know how exactly we can declare the class and how we can use it.
/*Example: Classes : InfoBrother:*/
using System;
namespace Classes
{
class display //class declared:
{
//Private Members | Variable or properties:
private int age = 22;
private string name = "Omar";
private double marks = 89.5;
//Public Member | Methods:
public void show()
{
Console.WriteLine("{0} is {1} Years Old:", name, age);
Console.WriteLine("{0} Got {1}% Marks in his C# Test:", name, marks);
}
}
class Program //another Class:
{
static void Main(string[] args) //main method:
{
//Object creation of class display
display disp = new display();
//calling show() method of class display.
//using dot pointer along with object of class display.
disp.show();
Console.ReadLine();
}
}
}
Class:
Omar is 22 Years Old:
Omar Got 89.5% Marks in his C# Test: