4

Duplicate

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'!
}
Community
  • 1
  • 1
Kredns
  • 36,461
  • 52
  • 152
  • 203
  • I'm not really understanding whether the example is satisfied by a function that returns an instance of a class named 'crap',,, – Jimmy Apr 09 '09 at 02:28
  • Well maybe... if you know a way to return an instance of crap in the main method that would be cool, if not how can I mask void as crap. – Kredns Apr 09 '09 at 02:30

2 Answers2

11

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;
  • great answer. I wouldn't have thought of this. – dustyburwell Apr 09 '09 at 02:38
  • @Gabe that's exaclty what i've been doing and from ur answer and what i've read I can't do something like _using fooVoid = System.Void;_. I don't care why, but I just wanted to know if there is any workaround so I can have my own keyword to substitute void during my code. Thks! – StinkyCat Jan 14 '13 at 11:45
2

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

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789