array of UInt16, what's the suffix in C#?
Asked Answered
L

4

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).

Lonesome answered 7/11, 2011 at 20:1 Comment(2)
I wish C# would have suffixes for shorts! It seems only fair, since longs are L. Maybe there is a reason that I am unaware of.Ohmage
No need for the casts. And to my knowledge, there isn't a suffix.Fatsoluble
G
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 };
Gridiron answered 7/11, 2011 at 20:3 Comment(4)
ushort would be preferable, IMO. But other that that, agreed.Authentic
@Lasse d'oh! indeed. Fixed ;pAuthentic
This is correct, there is no shortcut prefix. See here. UInt16 is the same as ushort, BTW.Masochism
+1: thanks..sorry for the simple question but I'm learning C# these days :)Lonesome
R
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.

Radiotelegram answered 7/11, 2011 at 20:7 Comment(2)
@Marlon: Right you are; I've rearranged my answer in case some poor soul stumbles upon it in the future.Kalsomine
@Radiotelegram They should bring this to C# .NET asap.Lepage
H
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 };
Harvell answered 8/11, 2011 at 13:54 Comment(0)
D
0

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

Unfortunately no suffix for short.

Dust answered 8/11, 2011 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.