In modern C# programming, working with collections of data is one of the most essential skills for beginners and professionals alike. While arrays of primitive data types like int, double, and string are commonly used, real-world applications often require storing multiple objects of a user-defined class. This is where the concept of a C# Array of Class Object becomes extremely important.
An Array of Class Objects in C# allows developers to store multiple instances of a class inside a single array structure. This approach is widely used in object-oriented programming in C#, enabling developers to manage structured and complex data efficiently. Whether you are preparing for C# interview questions, building a desktop application, or developing enterprise software using the .NET framework, understanding arrays of objects is fundamental.
In this detailed tutorial, we will explore:
Understanding Arrays in C#
An array in C# is a fixed-size collection of elements of the same type stored in contiguous memory locations. Arrays are zero-indexed, meaning indexing starts from 0.
Basic syntax:
datatype[] arrayName = new datatype[size];
Example:
int[] numbers = new int[5];
This creates an integer array capable of storing 5 integer values.
A class in C# is a blueprint for creating objects. It defines properties (variables), methods (functions), constructors, and other members.
class Student
{
public int Id;
public string Name;
public void Display()
{
Console.WriteLine("ID: " + Id);
Console.WriteLine("Name: " + Name);
}
}
Here, Student is a user-defined class. Now imagine storing multiple Student objects. Instead of creating separate variables, we can use a C# Array of Class Object.
An Array of Objects in C# is an array where each element is an instance of a class. Instead of storing primitive data types, it stores references to objects.
Syntax:
ClassName[] arrayName = new ClassName[size];
Example:
Student[] students = new Student[3];
Important: This line only creates an array that can hold 3 Student references. It does NOT create 3 Student objects automatically.
class Employee
{
public int EmpId;
public string EmpName;
public double Salary;
public void Display()
{
Console.WriteLine("Employee ID: " + EmpId);
Console.WriteLine("Employee Name: " + EmpName);
Console.WriteLine("Salary: " + Salary);
}
}
class Program
{
static void Main()
{
Employee[] empArray = new Employee[2];
empArray[0] = new Employee();
empArray[0].EmpId = 101;
empArray[0].EmpName = "Rahul";
empArray[0].Salary = 50000;
empArray[1] = new Employee();
empArray[1].EmpId = 102;
empArray[1].EmpName = "Sneha";
empArray[1].Salary = 60000;
for(int i = 0; i < empArray.Length; i++)
{
empArray[i].Display();
}
}
}
This is a complete C# Object Array Example demonstrating how to declare, initialize, and access array elements.
Understanding memory is important for mastering C# OOP Concepts.
So when you write:
Employee[] empArray = new Employee[2];
Only space for 2 references is created, not the objects.
Objects are created when:
empArray[0] = new Employee();
Instead of assigning values manually, we can use constructors.
class Product
{
public int ProductId;
public string ProductName;
public Product(int id, string name)
{
ProductId = id;
ProductName = name;
}
public void Display()
{
Console.WriteLine(ProductId + " - " + ProductName);
}
}
Product[] products = new Product[2];
products[0] = new Product(1, "Laptop");
products[1] = new Product(2, "Mobile");
foreach(Product p in products)
{
p.Display();
}
This method improves code readability and maintainability.
You can access properties using index:
Console.WriteLine(products[0].ProductName);
Always ensure the object is initialized before accessing it to avoid NullReferenceException.
Student[] students = new Student[2];
for(int i = 0; i < students.Length; i++)
{
students[i] = new Student();
Console.WriteLine("Enter ID:");
students[i].Id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Name:");
students[i].Name = Console.ReadLine();
}
This approach is commonly asked in C# interview questions.
Arrays have fixed size, while List<T> can grow dynamically.
List<Employee> empList = new List<Employee>();
empList.Add(new Employee());
For dynamic applications, List is preferred. But arrays are faster for fixed-size collections.
Student[,] studentMatrix = new Student[2,2];
studentMatrix[0,0] = new Student();
studentMatrix[0,0].Name = "Amit";
This structure is useful in grid-based systems.
In enterprise C# programming tutorial scenarios, arrays of objects are widely used in console and desktop applications.
Whether you are a beginner learning C# Programming Tutorial content or preparing for advanced C# interview questions, mastering arrays of objects will strengthen your programming foundation.
By practicing different examples and real-world scenarios, you can confidently use Array of Objects in C# in enterprise-level applications.
C# is primarily used on the Windows .NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved