Topic : Classes and Objects In C# Programming Language:



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.



image: class and objects

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:
        }
    }


  • In Above Syntax:

    » Access-Specifier defines the Scope of a class members. A class can be variable or method. in C# there are five types of Access Specifiers.


    Access SpecifierDefinition:
    Public:The class Member that is defined as Public can be accessed by other class Member that is initialized outside the class. A public member can be accessed from anywhere even outside the namespace.
    Private:The Class member that is defined as Private, can't be accessed from outside the class. The Private access specifiers restrict the member variable or method to be called outside from the parent class.
    Protected:The Protected access specifier hides its member methods and variables from other classes. This type of variable or methods can only be accessed in child class. if we want to inherit some properties or method of one class to another, then we declare our method or variable as Protected:
    Internal:The Class member that is defined as internal, can be access within the Program that contain its declarations. we can access any internal declared member within the same namespace that contain its declaration. but its hide from other classes that is resides in other namespace. it is default, its mean if we didn't mentioned any access specifier, then "internal" will be kick in:
    Protected Internal:The protected internal access specifier allows its members to be accessed in derived class, containing class or classes within same application. However, this access specifier rarely used in C# programming but it becomes important while implementing inheritance.


    » Class is the keyword to declare class:
    » ClassName is the Name for class. we can choose any name for our class, but using the naming rules same as variable name.
    » Data-Type specifies the type of variable. it could be any valid data type like int or float.
    » Return-Type is the data type to be return by method if any. we can use int if the method will return it, if the method wont return anything, we can use void.



» 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();
        }
    }
}


  • Above there is very simple but real example of Classes and objects. Let's look at the steps we follow in our program to make class and objects:
    1) » First of all, We Create a Class display in the namespace Classes.
    2) » We Define some Private Members or Properties in the class display . Only the Members of same class can access these private members.
    3) » We create An Public Method show() in the class. This method can access the Private members of display class, because it is the member of same class.
    4) » In the Main() Method of Another Class Program, We create the Object disp of display class.
    5) » In the Next step, using the Object disp along with dot (.) Operator, we call the Public method of Display class.

    In just 5 simple steps, we create the class and use it in proper way. you can follow these steps too to create any type of class in C#.



















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement