Class and object in c# with real time example

In this tutorial I am going to discuss about the real time example of Class and object in c#.


What is OOPS..?
Object oriented programming (OOP) is a programming structure where programs are managed by objects, classes and it’s  action and logic. C# provides full support for object-oriented programming including encapsulation, inheritance, and polymorphism.
Class:




Class and object in c# with real time example



If we talking about the class in any language, class is just a user define data type use to represent the relation between the data and functions or methods.

Class can have many things like variable , function, properties. In other way we can say that class is a block of code where we can define the state and behavior of object, function and variables.

Note:By default class is internal.


See below Syntax Format:

Class class name
{

      Statements;
     ----------------
    ------------------

}

Here I am taking an example of c# class.

using System;   //namespace

class Employee
{
   //variables
    public string name;
    int age;
    string address;

    public string departmant()           //method define by me
    {
      return "I.T.";
    }
    static void Main(string[] args)      //Main method
     {

      Employee emp = new Employee();   //Object creation of Employee class
      emp.name = "Jhon";
      emp.age = 10;
      emp.address = "Delhi";
      emp.departmant();
      Console.WriteLine(emp.name+" "+emp.age+" "+emp.address+" "+emp.departmant());
      }                                                                             
}




In above example we can see that our method and variable are exits in class.

Object: An object is a basically an instance of class. It allocatd the memory to access the member of class. Object is a real time entity.
 If we want to access the class member we need to create object of that particular class.
In below example we take ‘Employee’ as a class then ‘emp’ are the object.

Syntex for Object:

ClassName objectname=new ClassName();

E.g.

Employee emp = new Employee();   //Object creation of Employee class
In the above syntax the ‘new’ operator is used to create object of a class. When we create an object, then system creates memory for data member and methods that are present in the class.






Previous
Next Post »

1 comments:

Click here for comments
Naveen Singh
admin
April 1, 2018 at 10:13 AM ×

this is one of best concept that differ from other blogs and site.....

Congrats bro Naveen Singh you got PERTAMAX...! hehehehe...
Reply
avatar