The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,224 other subscribers

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

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,
        }
        // ...
    }
}

One Response to “.NET/C#: an alias is not the same as a synonym: int versus Int32, etc (fun with enums and aliases part 1)”

  1. […] .NET/C#: an alias is not the same as a synonym: int versus Int32, etc (fun with enums and aliases pa… […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: