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#". To understand object and class concepts read this post carefully.


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




Class and object in c# with real time example

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

A class can have many things like variable, function, properties. In another way, we can say that class is a block of code where we can define the state and behavior of objects, 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 the above example, we can see that our method and variable are exits in class.

Object: An object is basically an instance of the class. It allocated the memory to access the member of the class. An object is a real-time entity.
 If we want to access the class member we need to create an object of that particular class.
In the 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 an object of a class. When we create an object, then the system creates the memory for data members and methods that are present in the class.

Important Point to remember 
An object is the base type of all classes in the dot net framework. This object can hold any kind of value, however, boxing will be done implicity and unboxing need type casting mechanism.

Also Read: 
                 Boxing and Unboxing With Simple Example
                 Lockdown SQL Course




2 comments

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

Highly Great solutions sir! thanku and keep posting


EmoticonEmoticon