k11
k11

Reputation: 167

How to initialize an array to 0 without using for loops in C#

I have an array called Buffer. I am using a for loop to initialize its contents to 0. How do I do it in a single statement in C#? I do not want to use the for-loop.

byte[] Buffer = new byte[50];
int arrC = 0;
// array initialization
for (arrC = 0; arrC < 50; arrC++)
{
    Buffer[arrC] = 0;
}

Upvotes: 3

Views: 2410

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156938

You don't, and you don't have to. The default value for a byte is 0.

Hence, if you create an array of type byte[], each item in the array has the default value 0.

Upvotes: 12

Related Questions