Brian Leeming
Brian Leeming

Reputation: 11730

In C# what is the default value of the bytes when creating a new byte array?

The answer to this question has eluded my search.

When I do this:

  var authToken = new byte[16]; 

What is the value of authToken[0]?

Is it null or zero?

Upvotes: 9

Views: 26682

Answers (3)

Mehmet Taha Meral
Mehmet Taha Meral

Reputation: 3843

If you would like to set empty array for Auto-Implemented Properties, you can do that way.

public byte[] Attachement { get; set; } = Array.Empty<byte>();

So when you create new class that this property belongs to, if it is empty, it will be store like 0x

Upvotes: 5

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98750

From Arrays (C# Programming Guide)

The default values of numeric array elements are set to zero, and reference elements are set to null.

Since byte represents integer values from 0 to 255, all elements are set to 0 in your authToken array.

Upvotes: 2

Ronald Meijboom
Ronald Meijboom

Reputation: 1564

The default value is 0

For more information about default values:

http://msdn.microsoft.com/en-us/library/83fhsxwc.aspx

Upvotes: 21

Related Questions