C# Array Fill() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In C#, Array.Fill writes the same value into every element of an array, or into a chosen range. It is a simple way to initialize or reset a buffer to one constant.

Unlike Clear (which writes defaults), Fill writes the value you choose—such as -1 or "n/a".

This is a static member of System.Array. You call it with the class name, such as Array.Fill(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.

Syntax

Pass the array and the value. Optionally pass start index and count.

C#
Array.Fill(nums, 0);
Array.Fill(nums, -1, 1, 3);
CallWhat it does
Array.Fill(nums, 7)Every element becomes 7.
Array.Fill(nums, 7, 1, 2)Sets two elements starting at index 1 to 7.

Available on modern .NET. For older frameworks, a loop may be required.

Library declarations
C#
public static void Fill<T>(T[] array, T value);
public static void Fill<T>(T[] array, T value, int startIndex, int count);

Parameters

ParameterDescription
arrayRequired. Array to write into.
valueRequired. Value assigned to each slot in range.
startIndex / countOptional. Limit the filled range.

Return Value

Return ValueDescription
voidElements are updated in place.

C# Examples

Example 1 — Fill All With One Value

Set every int to 7.

C#
using System;

class Program {
    static void Main() {
        int[] nums = new int[4];
        Array.Fill(nums, 7);
        Console.WriteLine(string.Join(", ", nums));
    }
}

How It Works

  1. Start zeros. new int[4] begins as 0,0,0,0.
  2. Fill 7. Every slot becomes 7.
  3. One call. No manual loop needed.
  4. Length same. Still four elements.

Example 2 — Fill Only a Slice

Change a middle window to -1.

C#
using System;

class Program {
    static void Main() {
        int[] nums = { 1, 2, 3, 4, 5 };
        Array.Fill(nums, -1, 1, 3);
        Console.WriteLine(string.Join(", ", nums));
    }
}

How It Works

  1. Start 1, count 3. Indexes 1–3 are written.
  2. Sentinel -1. Often used for “unknown” markers.
  3. Ends kept. 1 and 5 remain.
  4. Vs Clear. Clear would write 0, not -1.

Example 3 — Fill With a Placeholder

Assign the same string reference to each slot.

C#
using System;

class Program {
    static void Main() {
        string[] cells = new string[3];
        Array.Fill(cells, "n/a");
        Console.WriteLine(string.Join(" | ", cells));
      }
}

How It Works

  1. Reference fill. Each slot gets the same string reference.
  2. Readable placeholder. Better UX than null for display tables.
  3. Started null. new string[3] begins as nulls.
  4. Immutable strings. Sharing one literal is fine.

Frequently Asked Questions

Clear sets defaults (0/null). Fill sets a value you choose.
No.
Yes. Pass startIndex and count.
No. It writes into the existing array.
Yes. That is similar to clearing those slots.

Next: Find()

Return the first element that matches a predicate, or default if none match with System.Array.

Find() 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