Wednesday, April 20, 2011

How to create keywords/types in C#?


typedef in C#?

Is there a way to create actually keywords in C#. Like for example turning object x into a datatype like int? I guess I'm asking is there anything like a typedef for C# and if not how can I actually make my own types that look like this:

public static crap Main(string[] args)
    // Note 'crap' and not 'void'!
From stackoverflow
  • C# intentionally does not provide C/C++ style macros (using #define). You cannot invent these kind of language features and extend the language beyond types. There are good reasons they left this feature out. If you really need these kind of features, you can always run the C# source file through a C preprocessor and compile the resulting file with a C# compiler. You can also rely on other templating features provided by your environment (most likely Visual Studio T4 engine).

    Shog9 : typedef != macros.
    Lucas McCoy : T4 looks awesome and I love Hanselman's blog!
  • You can create synonyms for classes such as Int32 or String, but not int or void because those are keywords. The syntax looks like this:

        using foo = System.Int32;
    Lucas McCoy : This is great and can resolve naming conflicts, thanks!
    dustyburwell : great answer. I wouldn't have thought of this.


Post a Comment