5

I'm trying to initialize inline an array of UInt16. For int I can do the following:

int[] int_array = new[]{0,0,0,0};

meanwhile using UInt16 doesn't work without a cast:

UInt16[] uint16_array= new[]{(UInt16)0,(UInt16)0};

It's quite annoying do those casts. I was wondering if there is any suffix in C# to disambiguate the assignment (like 0.0f for float).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Heisenbug
  • 38,762
  • 28
  • 132
  • 190

4 Answers4

16

I don't think there is one, but why don't you do this instead

UInt16[] uint16_array= new UInt16[] { 0, 0, 0, 0 };
Corey Kosak
  • 2,615
  • 17
  • 13
5

C# doesn't have a type suffix for unsigned 16-bit integers. VB.NET does though, just for reference:

Dim number As UShort = 8US

Here's another resource that lists the different suffixes.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
2

Here's an even shorter way than Corey's:

ushort[] uint16_array = { 0, 0, 0, 0 };

(or)

UInt16[] uint16_array = { 0, 0, 0, 0 };
Ani
  • 111,048
  • 26
  • 262
  • 307
0

http://msdn.microsoft.com/en-us/library/aa664674(v=VS.71).aspx

Unfortunately no suffix for short.

Jeremiah Gowdy
  • 5,476
  • 3
  • 21
  • 33