CS Notes 6
CS Notes 6
Output
Class & Objects in C#
Objects in C#
Object is a basic unit of Object-Oriented Programming and represents real-
life entities. It can also refer to the instance of a class. An object of that
class is created. An object holds data and can access methods and their
properties. In C# an object consists of :
State: It is represented by attributes of an object, and reflects the
properties of an object.
Behaviour: It is represented by the methods of an object, and also
reflects the response of an object with other objects.
Identity: It gives a unique name to an object and enables one object to
interact with other objects.
Objects correspond to things found in the real world. For example, a
graphics program may have objects such as “circle”, “square”,
“menu”. An online shopping system might have objects such as
“shopping cart”, “customer”, and “product”.
Declaring Objects (Also called instantiating a class)
When an object of a class is created, the class is said to be instantiated. All
the instances share the attributes and the behavior of the class. But the
values of those attributes, i.e. the state are unique for each object. A single
class may have any number of instances.
Initialization of Object
The new keyword instantiates a class by allocating memory for a new
object and returning a reference to that memory. The new operator also
invokes the class constructor.
// Initialization of an object
using System;
// Class Declaration
public class Dog
{
// Instance Variables
String name;
String breed;
int age;
String color;
my age is: 5
When we create an object of the Dog class and pass the parameters in the
constructor. So it allocates memory for these different objects and their
address points with the class’s object as shown in the image.