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
What are objects in C#?
In C#, objects are instances of classes that represent real-world entities. An object is created from a class blueprint and contains actual data and behavior. Objects allow you to access and manipulate the members (fields, properties, and methods) defined in a class.
To access class members through an object, you use the dot (.) operator. This operator connects the object name with the member name, enabling you to set values, call methods, and interact with the object's functionality.
Syntax
Following is the syntax for creating an object and accessing its members −
ClassName objectName = new ClassName(); objectName.memberName = value; // Access field or property objectName.MethodName(); // Call method
Object Creation and Member Access
When you create an object, you instantiate the class using the new keyword. Here's how objects work −
Box b1 = new Box(); // Create object b1.height = 7.0; // Access field b1.getVolume(); // Call method
Example
The following example demonstrates how to create objects and use them to access class members −
using System;
namespace BoxApplication {
class Box {
private double length; // Length of a box
private double breadth; // Breadth of a box
private double height; // Height of a box
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 Boxtester {
static void Main(string[] args) {
// Creating two objects
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box();
double volume;
// using objects to call the 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 Object Initializers
C# also supports object initializer syntax for setting properties during object creation −
using System;
class Person {
public string Name { get; set; }
public int Age { get; set; }
public void DisplayInfo() {
Console.WriteLine($"Name: {Name}, Age: {Age}");
}
}
class Program {
static void Main() {
// Object initializer syntax
Person person1 = new Person { Name = "Alice", Age = 25 };
Person person2 = new Person { Name = "Bob", Age = 30 };
person1.DisplayInfo();
person2.DisplayInfo();
}
}
The output of the above code is −
Name: Alice, Age: 25 Name: Bob, Age: 30
Key Points About Objects
-
Objects are instances of classes created using the
newkeyword. -
Each object has its own copy of instance variables but shares methods.
-
Use the dot operator (.) to access object members.
-
Multiple objects can be created from the same class, each with different data values.
Conclusion
Objects in C# are instances of classes that encapsulate data and behavior. They provide a way to create multiple entities with the same structure but different values. Objects are accessed using the dot operator to interact with their members, enabling object-oriented programming principles in C#.
