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);
Call
What 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
Parameter
Description
array
Required. Array to write into.
value
Required. Value assigned to each slot in range.
startIndex / count
Optional. Limit the filled range.
Return Value
Return Value
Description
void
Elements 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));
}
}
Output
7, 7, 7, 7
How It Works
Start zeros.new int[4] begins as 0,0,0,0.
Fill 7. Every slot becomes 7.
One call. No manual loop needed.
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));
}
}
Output
1, -1, -1, -1, 5
How It Works
Start 1, count 3. Indexes 1–3 are written.
Sentinel -1. Often used for “unknown” markers.
Ends kept. 1 and 5 remain.
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));
}
}
Output
n/a | n/a | n/a
How It Works
Reference fill. Each slot gets the same string reference.
Readable placeholder. Better UX than null for display tables.
Started null.new string[3] begins as nulls.
Immutable strings. Sharing one literal is fine.
Frequently Asked Questions
Clear sets defaults (0/null). Fill sets a value you choose.