Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alias a built-in type in C#?

Tags:

So in C++, I'm used to being able to do:

typedef int PeerId; 

This allows me to make a type more self-documenting, but additionally also allows me to make PeerId represent a different type at any time without changing all of the code. I could even turn PeerId into a class if I wanted. This kind of extensibility is what I want to have in C#, however I am having trouble figuring out how to create an alias for 'int' in C#.

I think I can use the using statement, but it only has scope in the current file I believe, so that won't work (The alias needs to be accessible between multiple files without being redefined). I also can't derive a class from built-in types (but normally this is what I would do to alias ref-types, such as List or Dictionary). I'm not sure what I can do. Any ideas?

like image 855
void.pointer Avatar asked Jul 15 '10 19:07

void.pointer


People also ask

What is type alias in C?

typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.

What is an alias type?

Type alias is a name that refers to a previously defined type (similar to typedef). Alias template is a name that refers to a family of types.

Is C++ an alias of C#?

C++ is not the alias of C# programming language.

What is alias in C Plus Plus?

You can use an alias declaration to declare a name to use as a synonym for a previously declared type. (This mechanism is also referred to informally as a type alias). You can also use this mechanism to create an alias template, which can be useful for custom allocators.


1 Answers

You need to use the full type name like this:

using DWORD = System.Int32; 
like image 102
ChaosPandion Avatar answered Sep 18 '22 06:09

ChaosPandion