Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How is a new object created in C#?
In C#, creating objects is fundamental to object-oriented programming. An object is an instance of a class that represents a real-world entity. Objects allow you to access class members including fields, properties, and methods using the dot operator.
Syntax
Following is the basic syntax for creating a new object in C# −
ClassName objectName = new ClassName();
You can also initialize an object with constructor parameters −
ClassName objectName = new ClassName(parameter1, parameter2);
Object Creation Process
When you create an object using the new keyword, the following steps occur:
Memory is allocated on the heap for the new object
The object's constructor is called to initialize the object
A reference to the object is returned and stored in the variable
Accessing Object Members
Once an object is created, you access its members using the dot (.) operator −
objectName.fieldName = value; objectName.MethodName();
Example
using System;
class Box {
private double length;
private double breadth;
private double height;
public void setLength(double len) {
length = len;
}
public void setBreadth(double bre) {
breadth = bre;
}
public void setHeight(double hei) {
height = hei;
}
public double getVolume() {
return length * breadth * height;
}
}
class Program {
static void Main(string[] args) {
// Creating two objects
Box Box1 = new Box();
Box Box2 = new Box();
double volume;
// Using objects to call member functions
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// Box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// Volume of box 1
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}", volume);
// Volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
}
}
The output of the above code is −
Volume of Box1 : 210 Volume of Box2 : 1560
Using Constructor Parameters
You can create objects with parameterized constructors to initialize values during object creation −
Example
using System;
class Student {
public string name;
public int age;
// Parameterized constructor
public Student(string studentName, int studentAge) {
name = studentName;
age = studentAge;
}
public void DisplayInfo() {
Console.WriteLine("Name: {0}, Age: {1}", name, age);
}
}
class Program {
static void Main(string[] args) {
// Creating objects with constructor parameters
Student student1 = new Student("Alice", 20);
Student student2 = new Student("Bob", 22);
student1.DisplayInfo();
student2.DisplayInfo();
}
}
The output of the above code is −
Name: Alice, Age: 20 Name: Bob, Age: 22
Multiple Objects from Same Class
You can create multiple independent objects from the same class. Each object has its own separate memory space and maintains its own state −
Example
using System;
class Counter {
private int count = 0;
public void Increment() {
count++;
}
public int GetCount() {
return count;
}
}
class Program {
static void Main(string[] args) {
// Creating three separate counter objects
Counter counter1 = new Counter();
Counter counter2 = new Counter();
Counter counter3 = new Counter();
// Each object maintains its own state
counter1.Increment();
counter1.Increment();
counter2.Increment();
Console.WriteLine("Counter1: {0}", counter1.GetCount());
Console.WriteLine("Counter2: {0}", counter2.GetCount());
Console.WriteLine("Counter3: {0}", counter3.GetCount());
}
}
The output of the above code is −
Counter1: 2 Counter2: 1 Counter3: 0
Conclusion
Creating objects in C# involves using the new keyword followed by a constructor call. Each object is an independent instance with its own memory space and state. Objects provide access to class members through the dot operator, enabling interaction with methods, fields, and properties defined in the class.
