Home C# Arrays Methods Clear() C# Array Clear() Method Definition In C#, Array.Clear sets a range of elements to the default value for the element type: 0 for numbers, false for bool, null for reference types.
The array’s Length does not change. Clear is useful when you want to wipe data without allocating a new array.
This is a static member of System.Array. You call it with the class name, such as Array.Clear(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.
Syntax Pass the array, starting index, and how many elements to clear.
Array.Clear(nums, 0, nums.Length);
Array.Clear(nums, 1, 2); Call What it does Array.Clear(nums, 0, nums.Length)Clears every element. Array.Clear(nums, 1, 2)Clears two elements starting at index 1.
Clear does not shrink the array. Use Resize or a new array if you need a different length.
Library declarations public static void Clear(Array array, int index, int length); Parameters Parameter Description array Required. Array to modify. index Starting index of the range. length How many elements to set to default.
Return Value Return Value Description void The specified range is reset in place.
C# Examples 1. Basic Clear 2. Partial 3. References Example 1 — Clear an Entire Int Array Wipe all values to zero; length stays 4.
using System;
class Program {
static void Main() {
int[] nums = { 1, 2, 3, 4 };
Array.Clear(nums, 0, nums.Length);
Console.WriteLine(string.Join(", ", nums));
Console.WriteLine("Length: " + nums.Length);
}
} How It Works Defaults. Each int becomes 0.Length fixed. Still four slots.Full range. Index 0 and length 4 cover all.Same object. No reallocation occurred.Example 2 — Clear a Middle Range Zero only two middle elements.
using System;
class Program {
static void Main() {
int[] nums = { 1, 2, 3, 4, 5 };
Array.Clear(nums, 1, 2);
Console.WriteLine(string.Join(", ", nums));
}
} How It Works Start at 1. First cleared slot is index 1.Length 2. Indexes 1 and 2 become 0.Ends kept. 1, 4, and 5 remain.Surgical wipe. Useful for clearing a buffer window.Example 3 — Clear Strings to Null Reference-type slots become null.
using System;
class Program {
static void Main() {
string[] names = { "Ann", "Bob", "Cara" };
Array.Clear(names, 0, names.Length);
Console.WriteLine(names[0] == null);
Console.WriteLine(names[1] == null);
Console.WriteLine("Length: " + names.Length);
}
} How It Works Null defaults. String elements become null.Not empty strings. null ≠ "".Length unchanged. Three slots still exist.GC friendly. Clearing drops references so objects can be collected if unused elsewhere.Frequently Asked Questions Does Clear change Length? No. It only sets elements in a range to default values.
What is the default for int and string? int becomes 0. string (and other reference types) become null.
Can I clear only part of an array? Yes. Pass index and length for the range.
Is Clear the same as Resize to 0? No. Resize(ref arr, 0) replaces the variable with a length-0 array. Clear keeps the length.
Does Clear allocate? It updates elements in place; it does not allocate a new array.
Next: Clone() Make a shallow copy of the array as an object with System.Array.
Clone() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion