[C# Tutorial 4] Classes and Objects


In previous tutorials we learned about operators and their uses, now lets see where they are written in a program, i.e CLASSES.

In Object oriented programming languages like C Sharp, Java, C++, methods, variables are  defined inside the classes, Therefore while coding at and foremost thing which comes is class, So,how to create it…!! The syntax is given below:


[attributes][access-modifiers] class identifier [:[base-class [,interface(s)]]

 Syntax consists of 6 parts,

  1. Attributes : They are the special keywords used for declaring different types of classes. e.g abstract.
  2. Access- Modifier :An access modifier determines the visibility of  the class. There are 5 types of access modifiers as follows.
    • Public
    • Private
    • Protected
    • Internal
    • Protected Internal.

    The detailed information on these access modifier will be given in the later tutorials.

  3. Class: This keyword is mandatory while declaring class.
  4. Identifier : It indicates the name of the class, by which the class is known.
  5. [:[base-class [,interface(s)]] : It is used when a SINGLE class or MULTIPLE interfaces are inherited. It is used in inheritance which will be discussed in later tutorials.
  6. Class body : The class body consists of its member functions and variables.

[] in the syntax are optional.


Classes are written inside a particular package, Therefore two classes inside 1 package can not have same name, while if both classes are in different packages then they are accesses using package_name.class_name.

Classes were introduced in Object Oriented languages for Data Encapsulation or Data Hiding,


Objects: Objects are declared on Classes, It acts as variable, and class on which it is defined act as Data Type (if any doubt in this plz comment it).

Syntax for creating Object is as follows:

Class_name object_identifier = new Class_name();
Syntax consists of 4 parts
  1. Class_name: It represents the name of the class on which the object is to be created.
  2. object_identifier : Every object created must have a unique name or identifier. Which is later used for accessing the member functions of the class.
  3. new : It is the keyword which need to be used for allocating the memory for that object.
  4. class_name: The same class_name need to be written after new keyword on which the object is to be made, but after class name there must be a opening and closing bracket ‘()’ followed by semicolon.

To understand the relationship between the classes and Objects, we can frame following statements,

  1. Objects are instances of class.
  2. More than 1 object can be created on a class.
  3. Class act as blue print on which objects are created.


For your better understanding we will use the Example which we used in tutorial 2[data types and variables] which displays the name, total marks  and  percentage secured.

Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1                   //Package name
class Program                                           //Main class
static void Main(string[] args)                  //Main Method

Console.WriteLine(“************Classes and Objects**************\n\n\n\n“);
add a = new add();                                //Creation of Object

Console.WriteLine(“Object Created“);
a.perform_add();     //Calling of member function ‘perform_add ‘of class add via object

public class add                                          //Class Declared with name ‘add’
{                                                                //Start of class body
public void perform_add()                           //Member function of class
String name = “Sachin“;
int sub1 = 65;
int sub2 = 75;
int total = sub1 + sub2;
double percentage = total * 100 / 200;
Console.WriteLine(name + ”  scored total ” + total + ” marks out of 200 “);
Console.WriteLine(“Percentage secured : ” + percentage);
if (percentage > 60)
Console.WriteLine(“1st Class“);
Console.WriteLine(“2nd Class“);
}                                                                   //End of class body




With this we have completed most of the classes and objects, some of the part like inheritance and polymorphism will be covered in later tutorials soon.


Leave A Reply