C# Array Clear() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

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.

C#
Array.Clear(nums, 0, nums.Length);
Array.Clear(nums, 1, 2);
CallWhat 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
C#
public static void Clear(Array array, int index, int length);

Parameters

ParameterDescription
arrayRequired. Array to modify.
indexStarting index of the range.
lengthHow many elements to set to default.

Return Value

Return ValueDescription
voidThe specified range is reset in place.

C# Examples

Example 1 — Clear an Entire Int Array

Wipe all values to zero; length stays 4.

C#
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

  1. Defaults. Each int becomes 0.
  2. Length fixed. Still four slots.
  3. Full range. Index 0 and length 4 cover all.
  4. Same object. No reallocation occurred.

Example 2 — Clear a Middle Range

Zero only two middle elements.

C#
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

  1. Start at 1. First cleared slot is index 1.
  2. Length 2. Indexes 1 and 2 become 0.
  3. Ends kept. 1, 4, and 5 remain.
  4. Surgical wipe. Useful for clearing a buffer window.

Example 3 — Clear Strings to Null

Reference-type slots become null.

C#
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

  1. Null defaults. String elements become null.
  2. Not empty strings. null ≠ "".
  3. Length unchanged. Three slots still exist.
  4. GC friendly. Clearing drops references so objects can be collected if unused elsewhere.

Frequently Asked Questions

No. It only sets elements in a range to default values.
int becomes 0. string (and other reference types) become null.
Yes. Pass index and length for the range.
No. Resize(ref arr, 0) replaces the variable with a length-0 array. Clear keeps the length.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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