Brief Lesson About C# Classes And Objects

Synopsis

  1. Introduction
  2. Definition
  3. Class Description
  4. Object Description
  5. Accessing Class Prosperities using Object
  6. Conclusion

Introduction

This article explains about class and object in a detailedway. It is common for all programming Languages except for syntax. Concept wise all are same way for all programming languages.

Definition

Class is a collection of member data and member function or member method, which means collection of variables and methods.

Object is a run time entity that is used to access class variables and methods.

Class and Object Description

Class is a structure of memory allocation method of programming languages. It is the same for all programming Languages. Class has the different data type of variables and methods. It is used to manipulate data with protected way. We can manipulate data without class but it is not a protected way and it is not a fast accessing way.

Class is a raw data, without object it is not complete full structure.

Real time Example
example
Explanation

Above diagram explains about class and object. If you need to make a brick you need the raw material as well as the shape. Without raw material or shape we could not make a brick, both are very important.

Class is a like raw material and object is a like shape. Without object cannot access class as well as without class could not create object both are very important.

example
Class Syntax

Using “class” Keyword to creates class. Class has following structure.

  1. Class Syntax  
  2. Using “class” Keyword to creates class. Class has following structure.  
  3. class  class_name  
  4. {  
  5.     //member variable and member function.  
  6. }  
Example
  1. Public class Sample  
  2.     {  
  3.         public int Rno;  
  4.         public string Name;  
  5.     }  
Classes have good security method while accessing class variables and methods with the help of object. Accesses specifier or modifier used to secure access in class. One of the main advances of class is security. Before discovering the class do not having good secured access.

The following diagram explains about memory allocations for above class.
example

Object Creation

Object is a runtime entity. It is used to access class proprieties. Creating objects following way.
  1. static void Main(string[] args)  
  2.       {  
  3.             Sample ss = new Sample(); // Object Creation  
  4.       }  
example

Access Class Prosperities

Accessing class prosperities using object in following way.
  1. public class Sample  
  2.    {  
  3.        public int Rno;  
  4.        public string Name;  
  5.    }  
  6.   
  7.    class Program  
  8.    {  
  9.        static void Main(string[] args)  
  10.        {  
  11.            Sample ss = new Sample();  
  12.              
  13.            ss.Rno = 1001;  
  14.            ss.Name = "Demo";  // Accessing Class Properities Using Object  
  15.        }  
  16.    }  
example
Example Program
  1.      namespace OopsFirstClass  
  2.     {  
  3.     public class Sample  
  4.     {  
  5.         public int Rno;  
  6.         public string Name;  
  7.     }  
  8.   
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Sample ss = new Sample();  
  14.               
  15.             ss.Rno = 1001;  
  16.             ss.Name = "Demo";  
  17.   
  18.             Sample ss1 = new Sample();  
  19.             ss1.Rno = 1002;  
  20.             ss1.Name = "Finish Deom";  
  21.   
  22.             Console.WriteLine("Rno:" + ss.Rno);  
  23.             Console.WriteLine("Name:" + ss.Name);  
  24.   
  25.             Console.WriteLine("Rno:"+ ss1.Rno);  
  26.             Console.WriteLine("Name:" + ss1.Name);  
  27.             Console.Read();                
  28.         }  
  29.     }  
  30. }  
Conclusion

This article helps those who are learning OOPS concepts newly as well as students. Next part of article explains about the next topic of oops concept.


Similar Articles