Typedef for C# (Type Alias)

so int is a alias for System.Int32. How can I add my own alias that will be GLOBAL. So instead of

List<int> lLanguageIDs;

I could do

List<LanguageID> lLanguageIDs;

where LanguageID is a alias to int or System.Int32

thanks

Ralph



Answer this question

Typedef for C# (Type Alias)

  • FlyFisher

    Only way to do something like this globally is via subclassing

    public class MyDictionary : Dictionary<string, string> {}

    Not what you're looking for I know, works though. C# Generics cause havok with my attempts at an 80-ish line width.

    Luke

  • Matt Warren

    There is no global way to define an alias in the way you described there that I am aware of... you *might* be able to setup your own external alias in a roundabout way with the extern alias keyword... however for the life of me I cannot see how it all would come together.

  • John-at-WSRB

    Thanks, I do know about that (I should have said that) but is only works per file. I am looking for something global......

    Perhaps System.Int32.AddAlias("LanguageID");

    something like that...


  • Pierce Blaylock

    You can by using one of the not well known features of the using directive where you can use it to define a type name of your choice that maps to another type, unfortunately you cannot do this explicitly with C# types, instead you must index CLR types ala:

          using LanguageID = System.Int32;

    And with that, your your final example will work.



  • Typedef for C# (Type Alias)