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'! } 
2
  • I'm not really understanding whether the example is satisfied by a function that returns an instance of a class named 'crap',,, Commented Apr 9, 2009 at 2: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. Commented Apr 9, 2009 at 2:30

2 Answers 2

12

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; 
Sign up to request clarification or add additional context in comments.

2 Comments

great answer. I wouldn't have thought of this.
@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!
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.