An array of class objects in C# is a collection that allows you to store multiple instances of a class. This is useful when you need to manage multiple objects of the same type.
Example
Defining a Class
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Department { get; set; }
public Employee(int id, string name, string department)
{
Id = id;
Name = name;
Department = department;
}
}
Declaring and Initializing an Array of Objects
1.Using Object Initializers
Employee[] employees = new Employee[]
{
new Employee(1, "John Doe", "HR"),
new Employee(2, "Jane Smith", "Finance"),
new Employee(3, "Mike Brown", "IT")
};
2.Assigning Values After Declaration
Employee[] employees = new Employee[3]; // Declare an array with a size of 3
employees[0] = new Employee(1, "John Doe", "HR");
employees[1] = new Employee(2, "Jane Smith", "Finance");
employees[2] = new Employee(3, "Mike Brown", "IT");
3. Accessing Array Elements
You can access elements of the array using their index:
Console.WriteLine($"Employee Name: {employees[0].Name}, Department: {employees[0].Department}");
4. Iterating array
// foreach
foreach (var employee in employees)
{
Console.WriteLine($"ID: {employee.Id}, Name: {employee.Name}, Department: {employee.Department}");
}
//for loop
for (int i = 0; i < employees.Length; i++)
{
Console.WriteLine($"ID: {employees[i].Id}, Name: {employees[i].Name}, Department: {employees[i].Department}");
}
An array of class objects in C# is a collection that allows you to store multiple instances of a class. This is useful when you need to manage multiple objects of the same type.
Example
Defining a Class
public class Employee { public int Id { get; set; } public string Name { get; set; } public string Department { get; set; } public Employee(int id, string name, string department) { Id = id; Name = name; Department = department; } }
Declaring and Initializing an Array of Objects
1.Using Object Initializers
Employee[] employees = new Employee[] { new Employee(1, "John Doe", "HR"), new Employee(2, "Jane Smith", "Finance"), new Employee(3, "Mike Brown", "IT") };
2.Assigning Values After Declaration
Employee[] employees = new Employee[3]; // Declare an array with a size of 3 employees[0] = new Employee(1, "John Doe", "HR"); employees[1] = new Employee(2, "Jane Smith", "Finance"); employees[2] = new Employee(3, "Mike Brown", "IT");
3. Accessing Array Elements
You can access elements of the array using their index:
Console.WriteLine($"Employee Name: {employees[0].Name}, Department: {employees[0].Department}");
4. Iterating array
// foreach foreach (var employee in employees) { Console.WriteLine($"ID: {employee.Id}, Name: {employee.Name}, Department: {employee.Department}"); } //for loop for (int i = 0; i < employees.Length; i++) { Console.WriteLine($"ID: {employees[i].Id}, Name: {employees[i].Name}, Department: {employees[i].Department}"); }
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