You are on page 1of 3

Object and Class in C#

In this article, we will learn about C# objects and classes.


In object-oriented programming technique, we design a program using objects and classes.
Object is the physical as well as logical entity whereas class is the logical entity only.

Objects in C#:
An entity that has state and behavior is known as an object e.g. pen, table, car etc. It can be
physical or logical .
An object has three characteristics:

state: represents data (value) of an object.

behavior: represents the behavior (functionality) of an object such as deposit,


withdraw etc.

identity: Object identity is typically implemented via a unique ID. The value of the ID
is not visible to the external user. But,it is used internally by the JVM to identify each
object uniquely.

For Example: Pen is an object. Its name is Parker, color is black etc. known as its state. It is
used to write, so writing is its behavior.
Object is an instance of a class. Class is a template or blueprint from which objects are
created. So object is the instance(result) of a class. For example, you have a class called
Vehicle and car is the object of that class.

Classes in C#:

1 namespace HelloWorld

2
3
{
4
class firstProgram
5
{
6
public static void Main(string[] args)
7
{
8
Console.WriteLine("Hello World !!");
9
1
}
0
1
}
1
}
1
2

In the above example ,

we have a single class named FirstProgram that contains a single member a


method named Main.

Every C# application must define a Main method in one of its classes.

The public keyword is an access modifier that tells the C# compiler that any code
can call this method.

The static modifier tells the compiler that the Main method is a global method and
the class doesnt need to instantiated for the method to be called.

The given code shows the Main method as returning void and not receiving
anyarguments. However, you can define Main method to return a value and take
array of arguments

A class in C# can contain:

data member

properties

constructor

methods

Notes:

Class name should start with uppercase letter and be a noun e.g. String, Color,
Button, System, Thread etc.

The name of the constructor is always same as the class name

A class can have any number of data members, properties, constructors


and methods

Data member defined using a class is called as object reference.

A class can have a data member which is an object reference of the same class Like
the manager of the employee is also a employee.

Example:

1 class Employee
2{
3 int empNo;
4 string empName;
5 Employee Manager;
6
7
8}

2016, admin. All rights reserved.

You might also like