Greg Dolley’s Weblog

A Blog about Graphics Programming, Game Programming, Tips and Tricks

Can I Use Typedef’s in C#?

Posted by gregd1024 on February 15, 2008

When moving from C++ to C#, the following question often arises: does C# support typedef’s? Unfortunately, no. However, you can get pretty close. Consider this code:

using GLenum = System.UInt32;

The “using GLenum” statement makes the symbol “GLenum” equivalent to a “System.UInt32” name. Now you can use “GLenum” to declare identifiers instead of “UInt32.” In this way, it’s acting exactly like a C or C++ typedef. What’s actually happening is that you’re creating a C# alias – a mechanism which allows the renaming of a class, struct, or namespace. While this may seem pretty flexible, consider what happens when you try the following:

using GLfloat = float; // ERROR!

This line results in a compile error because “float” is a keyword. Why weren’t C# aliases built to support this as well? When you consider the original purpose of aliases it kind of makes sense. They were meant to save the programmer from lots of typing by providing a mechanism to shorten long qualifiers. We all know some namespaces and classes in C# are much longer than what you would typically see in a C++ program, so providing renaming support was practically a necessity. But do you really think a lot of programmers are going to shorten “float” or “int”? So when I said you can “get pretty close” to a C++ typedef, I was specifically referring to limitations such as this.

Another big limitation is that aliases are limited in scope. Only the namespace in which they’re declared can see them and their scope cannot cross multiple source files. The last part is severely limiting – it forces you to include the same declaration in every file. At least it’s better than nothing.

-Greg Dolley

*Get new posts automatically! Grab the RSS feed here. Want email updates instead? Click here.

Advertisement

8 Responses to “Can I Use Typedef’s in C#?”

  1. ac said

    linux kernel developer: (http://en.wikipedia.org/wiki/Typedef)

    “[typedefs] not only unnecessarily obfuscates code, it can also cause programmers to accidentally misuse large structures thinking them to be simple types”

    The stated intent is to “is to make it easier for programmers to comprehend source code”.

    I’ve done some C/C++ porting but my background is C#. There is little doubt in my mind that typedefs made the code harder to understand when I needed to.

    You don’t say why would you want to have this feature in C#? It could make it easier for porting legacy code to C#. So now you have some obfuscated C# … Now we might give C# all the goodies from C++ and then call it C++/CLI but would we prefer using this language over C#? I think you’ll find more C++ devs who like C# than C++/CLI if you asked around.

  2. bobobobo said

    Well, you can shorten float by using

    using GLfloat = System.Single ; // typeof(System.Single) == typeof(float)

    Also, if you wanted to bring an enumerated type into C# from somewhere else, you’d better do something like

    public enum GLenum : uint
    {
    // list values
    }

    Which says that GLenum’s underlying type is unsigned int

  3. CVA said

    “[typedefs] not only unnecessarily obfuscates code, it can also cause programmers to accidentally misuse large structures thinking them to be simple types”

    The stated intent is to “is to make it easier for programmers to comprehend source code”.
    =====================================================================================================

    Typedefs are actually meant to support one of the most important principles of programming: “Single Point Control”.

    If GLenum one day should be ulong instead of uint it would (if used correctly) only require change to one file.

  4. […] be used to create an alias for a type which appears to be a pretty cool replacement for typedef, this article brings out some interesting and important differences. Mainly, the need to define the alias in […]

  5. Su2anna said

    what is short equivalent in c#..???

    (c/c++–>>short) == (c#–>System.Int16):?

  6. Su2anna said

    does it mean like this

    in C

    short x;// takes 2 Byte

    when i use x in C#

    short x;// takes 2 Byte

    …………………………….???

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

 
%d bloggers like this: