![]() |
VOOZH | about |
A fully object-oriented language means everything is represented as an object but can't differentiate between primitive types and objects of classes but C# is not purely object oriented since it supports many procedural programming concepts such as pointers to an extent. An object is a basic unit of Object Oriented Programming and represents the real-life entities. A typical C# program creates many objects, which as you know, interact by invoking methods. We can Create objects in C# in the following ways:
1) Using the 'new' operator: A class is a reference type and at the run time, any object of the reference type is assigned a null value unless it is declared using the new operator. The new operator assigns space in the memory to the object only during run time which means the allocation is dynamic.
Syntax:
// The className() is a call // to the constructor className ObjectName = new className();
Note: The constructor can be a default constructor or a user-defined one.
Example:
The area of the Rectangle is 120
2) Creating Reference to Existing Object: The reference can be declared only with the class name and reference name. The reference cannot exist independently. It has to be assigned to an already existing object of the same class. Any changes made in the reference will be saved to the object it is referring to. It is kind of like an alias.
Syntax:
className RefName; RefName = objectName;
Example:
Area of tri1 is 0 Area of tri1 is 17.5
3) Creating an Array of objects: If you need the multiple numbers of objects of the same class you can create an array of objects. This will require you to declare the array first and then initialize each element { object in this case }. You can use for loop for initialization.
Syntax:
className[] arrayName = new className[size];
Area of circle with radius 1 is 3.14 Area of circle with radius 2 is 12.56