This example shows an array of objects, each with 2 properties. The Array.Clear method is used to set the first 2 references in the array to null.
using System;
class Program
{
class Employee
{
public string Name { get; set; }
public int Salary { get; set; }
}
static void Main()
{
Employee[] employees = new Employee[3];
employees[0] = new Employee() { Name =
"Bob", Salary = 10000 };
employees[1] = new Employee() { Name =
"Susan", Salary = 13000 };
employees[2] = new Employee() { Name =
"John", Salary = 20000 };
//
// Display the employee array.
//
Console.WriteLine(
"--- Employee array before ---");
foreach (Employee employee in employees)
{
Console.Write(employee.Name);
Console.Write(
": ");
Console.WriteLine(employee.Salary);
}
//
// Clear first two elements in employee array.
//
Array.Clear(employees, 0, Math.Min(2, employees.Length));
//
// Display the employee array.
//
Console.WriteLine(
"--- Employee array after ---");
foreach (Employee employee in employees)
{
if (employee != null)
{
Console.Write(employee.Name);
Console.Write(
": ");
Console.WriteLine(employee.Salary);
}
else
{
Console.WriteLine(
"null");
}
}
}
}
--- Employee array before ---
Bob: 10000
Susan: 13000
John: 20000
--- Employee array after ---
null
null
John: 20000