How to Declare an Array of Objects in C#
-
Array of Objects in
C#
-
Declare an Array of Objects With String Data Type in
C#
-
Declare an Array of Objects With Float and Integer Data Type in
C#
This article will introduce how to declare or initialize an array of objects. Using an array of objects allows us to access class methods with each object.
Array of Objects in C#
Object arrays can be used in various ways; they keep components of many sorts together in a single collection. An object reference can reference any derived type instance.
Here are some examples of object array declarations in C#.
Declare an Array of Objects With String Data Type in C#
Syntax:
employee[] e = new employee[3];
e[0] = new employee();
e[1] = new employee();
e[2] = new employee();
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1 {
class employee {
private string empName;
private int empId;
private string empDesig;
public void EmpInfo(string empName, int empId, string empDesig) {
this.empId = empId;
this.empDesig = empDesig;
this.empName = empName;
}
public void showEmployeeDetails() {
Console.WriteLine("\nEmployee Record: ");
Console.WriteLine("\t Emp Name: " + empName);
Console.WriteLine("\t Id : " + empId);
Console.WriteLine("\tDesignation : " + empDesig);
}
}
class EmployeeTest {
public static void Main() {
employee[] e = new employee[3];
e[0] = new employee();
e[1] = new employee();
e[2] = new employee();
e[0].EmpInfo("Shan", 132, "Manager");
e[0].showEmployeeDetails();
e[1].EmpInfo("Casper", 131, "CEO");
e[1].showEmployeeDetails();
e[2].EmpInfo("Olga", 139, "Team Leader");
e[2].showEmployeeDetails();
}
}
}
Output:
Employee Record:
Emp Name: Shan
Id : 132
Designation : Manager
Employee Record:
Emp Name: Casper
Id : 131
Designation : CEO
Employee Record:
Emp Name: Olga
Id : 139
Designation : Team Leader
By following these steps, we can declare an array of objects.
- First, we must import the following libraries, which are required.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
- Now, create a class named
employee
, and inside the class, we’ll declare 3 private data members of theemployee
.
class employee {
private string empName;
private int empId;
private string empDesig;
}
- Create a constructor named
EmpInfo()
inside theemployee
class and pass 3 arguments namedempName
,empId
,empDesig
, respectively. TheEmpInfo()
is a setter method to set the private variable values of theemployee
object, which are not accessible outside theemployee
class.
public void EmpInfo(string empName, int empId, string empDesig) {
this.empId = empId;
this.empDesig = empDesig;
this.empName = empName;
}
- We built the
showEmployeeDetails()
function inside theemployee
class to display employee details. We built this method to displayemployee
data on the console becauseemployee
variables are unavailable outside the class.
We can directly call showEmployeeDetails()
on our employee
object to display employee
data in the console due to the availability of this function.
public void showEmployeeDetails() {
Console.WriteLine("\nEmployee Record: ");
Console.WriteLine("\t Emp Name: " + empName);
Console.WriteLine("\t Id : " + empId);
Console.WriteLine("\tDesignation : " + empDesig);
}
- We’ll make an array of objects in the
Main
class. All objects for theemployee
class will be stored in the array we’ve constructed. Then we’ll create and initialize two objects.
After that, we’ll call the constructor we previously created within our employee
class with a value for each variable for both of our objects.
Then, within our employee
class, we’ll call showEmployeeDetails()
to print the values of each employee
that we’ve assigned in the preceding code block.
class EmployeeTest {
public static void Main() {
employee[] e = new employee[3];
e[0] = new employee();
e[1] = new employee();
e[2] = new employee();
e[0].EmpInfo("Shan", 132, "Manager");
e[0].showEmployeeDetails();
e[1].EmpInfo("Casper", 131, "CEO");
e[1].showEmployeeDetails();
e[2].EmpInfo("Olga", 139, "Team Leader");
e[2].showEmployeeDetails();
}
}
Declare an Array of Objects With Float and Integer Data Type in C#
Syntax:
object[] num = new object[5];
num[0] = 2.15;
num[1] = 'S';
num[2] = 27;
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class DeclareObjectArray {
static public void Main() {
object[] num = new object[5];
// float value
num[0] = 2.15;
// Character
num[1] = 'S';
// integer value
num[2] = 27;
// null value
num[3] = null;
// system object
num[4] = new object();
foreach (var Items in num) {
Console.WriteLine(Items);
}
}
}
Output:
2.15
S
27
System.Object
In the Main
class, we’ll create an array of objects for this example.
The array we’ve created will hold all of the objects for the num
class. Then we’ll make 5 objects and set them up.
After that, we’ll execute the constructer we created before with a value for each variable for all of our instances.
I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.
LinkedIn