10

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?

5
  • 2
    Why not write a line of code and use the debugger? Commented Mar 19, 2014 at 12:47
  • I think it's warning or error at complie time. You should give it a try. If it's a local variable. Commented Mar 19, 2014 at 12:47
  • 6
    A byte cannot be null. Commented Mar 19, 2014 at 12:48
  • 1
    byte b = default(byte) Commented Mar 19, 2014 at 12:49
  • 2
    @HåkanFahlstedt because that just tells me on my PC in that particular point in time, it was zero. It doesn't guarantee that it would be zero in production. Commented Mar 19, 2014 at 13:10

3 Answers 3

21

The default value is 0

For more information about default values:

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

Sign up to request clarification or add additional context in comments.

1 Comment

And following the link, critically, there is this text: Remember that using uninitialized variables in C# is not allowed.
5

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

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.