.NET/C#: an alias is not the same as a synonym: int versus Int32, etc (fun with enums and aliases part 1)
Posted by jpluimers on 2014/01/28
I was amongst the C# programmers that believe the below table of C# integral types is an alias table. But it is not: it is a synonym table.
C# type | .NET Framework Type/ Common Type System |
---|---|
byte | System.Byte |
sbyte | System.SByte |
short | System.Int16 |
ushort | System.UInt16 |
int | System.Int32 |
uint | System.UInt32 |
long | System.Int64 |
ulong | System.UInt64 |
You can use the common type system types as coding standard, or prefer the C# types. There are arguments for both.
I am still in the first group: prefer CTS types. They make changing between .NET languages a lot easier (C# and VB.NET are not the only .NET based CLI languages I use).
And indeed almost everywhere you can exchange the .NET Framework Type and the C# type without changing meaning.
There are two exceptions
- “int” can be used as an underlying type for enums; Int32 can’t.
- Alias declarations can’t use keywords.
I found about the former the hard way when doing quite a bit of P/Invoke work trying to keep type names as close to the Windows API ones as possible, see the question below.
The issue is marked as “Won’t fix” at Microsoft Connect, and Eric Lippert explains how it got there in the first place.
In the process trying to permutate data types to find the root cause, I also used aliases. And when trying out the permutations, I also found the second exception.
A few observations
- Jon Skeet is hardly wrong, but the “They may be used interchangeably” part in the above Alias Table link is wrong.
- Many people think the type after an enum is the ancestor type. It is the
Fazit
Don’t mix the words synonym and alias. They have close, but different meaning.
–jeroen
StackOverflow question that lead to all this
After asking c# – Why can’t enum types be underlying types that are integral types in the System namespace? – Stack Overflow, I also found these related questions:
- .net – C# int, Int32 and enums – Stack Overflow.
- variable types – C#, int or Int32? Should I care? – Stack Overflow.
- c# – Why can’t I declare an enum inheriting from Byte but I can from byte? – Stack Overflow.
This was the question that started it:
I’ve done quite a bit of PInvoke stuff recently, wanting to stay close to the Windows API header files.
When creating the enum below, you get this compiler error:
Error 1 Type byte, sbyte, short, ushort, int, uint, long, or ulong expected
Why is it that the C# compiler does not understand any other underlying types?
The C# compiler can go from uint to System.UInt16, why not the other way around?using System; using System.Runtime.InteropServices; using DWORD = System.UInt32; namespace BeSharp.Win32 { [StructLayout(LayoutKind.Sequential)] internal class MOUSEINPUT { // ... MouseData mouseData; // DWORD // ... [Flags] internal enum MouseData : System.UInt32; // needs to be uint; you cannot do DWORD or System.UInt32 here { Nothing = 0x0000, XBUTTON1 = 0x0001, XBUTTON2 = 0x0002, } // ... } }
.NET/C#: fun with enums and aliases part 2 « The Wiert Corner – irregular stream of stuff said
[…] .NET/C#: an alias is not the same as a synonym: int versus Int32, etc (fun with enums and aliases pa… […]