Introduction to Objects and Classes in C#

By , March 1, 2011

In this article we will understand some of the concepts of object-oriented programming in C# like objects and classes. To read this article you must have C# programming basics.

Introduction:

OOP stands for Object-Oriented Programming. Before OOP programmers used to creating computer applications using procedural-programming (or structure-programming) but when OOP solved a lot of the problems of the procedural-programming so almost all of the programmers and developers began using OOP languages. In procedural- programming all the program functionality written in a few modules of code or maybe one module (depending on the program) and these modules depend on one another and maybe if you change a line of code you will have to rewrite the whole module again and maybe the whole program but in Object-Oriented Programming programmers write independent parts of a program called classes each class represent a part of the program functionality and these classes can be assembled to form a program and when you need to change some of the program functionality all what you have to do is to replace the target class which may contain a problem. So in OOP applications create by the use of classes and these applications can contain any number of classes. That will get us to discuss the Class and Object concept.

Classes and objects:

The class and object is related to each other, if you do not understand the clear concept of these two I think you will have a hard times learning C#.

In object-oriented programming , a class is a template definition of the methods and variables which define/describe/implement the characteristic of an entity, like what operation it would perform and how it will perform, how the implemented functionality can be used.

An object is a specific instance of a class; it contains real values and runs/uses the functionality implemented by class. It is kind of mirror of the class but with real values and ready to execute.

Properties and Variables:

Variables declared in a class store the data for each instance, means that when you instantiate this class (that is, When you create an object of this class) the object will allocate a memory locations to store the data of its variables. Let’s take an example to understand it well.

class Human
{

public string Gender;

public string Name;

}

This is our simple class which contains 2 variables. Now when you instantiate this class (below)

static void Main(string[] args)
{

Human Ashish = new Human();

Human Jolly = new Human();

// Specify some values for the instance variables

Ashish.Gender = “Male”;

Ashish.Name = “Ashish”;

Jolly.Gender = “Female”;

Jolly.Name = “Bhavana”;

// print the values on console’s screen

Console.WriteLine(“Ashish’s gender = {0}, and Jolly”s gender = {1}”,Ashish.Gender, Jolly.Gender);

Console.ReadLine();

}

So we begin our Main method by creating 2 objects of Human type. After creating the 2 objects we initialize the instance variables for object Ashish and then for object Jolly. Finally we print some values to the console. Here C# compiler allocate a memory location for the 2 instance variables for each object to put the values there. So each object now contains a different data.

Properties:

Properties is a way to access the variables of the class in a secure manner. Secure how? by implementing property you get two different code block 1) get and 2) set, each code block has its own purpose, the ‘get’ will be used to return the value to the caller, and the ‘set’ will be used to get the value from the caller and set it to the local variable.  For each property we always have a associated variable, this associated variable is used to store the value location on behalf of its property. you would see below that within the set code block we set the passed value to the location variable, and the within the get code block we return the value from its location variable. So to secure, you could also put the condition/peace of code to validate the given value before you set the value to its associated location variable, like below you would see we are validating the passed value before we set the value to ‘age’ variable. Let’s see the example:

class Person
{

private int age;

private string hairColor;

public int Age

{

get
{

return age;

}
set
{

if(value <= 65 && value >= 18)

{

age = value;

}
else

age = 18;

}

}
public string HairColor
{

get
{

return hairColor;

}
set
{

hairColor = value;

}

}

}

14 Responses to “Introduction to Objects and Classes in C#”

  1. Roberto says:

    .

    thanks for information!

  2. Souls in the Waves

    Very good Morning, I just stopped in to go to your internet site and imagined I would say I experienced myself.

  3. Stanley says:

    .

    ñïàñèáî çà èíôó.

  4. ricky says:

    .

    thanks for information!

  5. phillip says:

    .

    áëàãîäàðþ.

  6. sam says:

    .

    tnx for info!!

  7. herbert says:

    .

    áëàãîäàðþ!!

  8. Samuel says:

    .

    ñïàñèáî çà èíôó.