Merge in .NET.

Is it possible to create an alias for existing types and use this alias through a project?

For example, create

CustomerID = System.UInt32 

and use CustomerID as a data type?

Version: .NET Framework 4.0

(With the keyword "using", we could create an alias, but this is not useful since it does not work with files.)

Any other ideas?

+5
source share
2 answers

I believe the correct answer to this question is:

No, this (unfortunately?) Is impossible .: (


Edit 1: However, you can (kind of) mimic this by creating your own struct and providing implicit conversions to / from the type you need. This is not entirely magical, but it may work, depending on the situation.


Edit 2: I haven't used this feature before, but the forwarding type might be useful. However, it is only being forwarded to another whole assembly, so it probably won't work for simple cases.

+4
source

This is not possible. However, for what reason do you want to do this?

If your reason is <readability consider the following:

You can already use your identifiers to indicate that it is, for example, a customerID . What you essentially do when you try to rename UInt32 to customerID makes it look like a new type. Only when a type requires additional behavior or values ​​is it useful to consider it "new." If you've ever developed in C ++, you can appreciate the convention caused by not allowing it in C #. You know what to expect from UInt32 .

If your reason is preparing yourself for change :

eg. Suppose you decide after a while that you want to support negative identifiers. This must be resolved by encapsulating this possible expected behavior in a new type. Therefore, renaming is not required.

0
source

All Articles