Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting aliases in C#

Tags:

c#

alias

typedef

I've seen lots of answers to the typedef problem in C#, which I've used, so I have:

using Foo = System.Collections.Generic.Queue<Bar>;

and this works well. I can change the definition (esp. change Bar => Zoo etc) and everything that uses Foo changes. Great.

Now I want this to work:

using Foo = System.Collections.Generic.Queue<Bar>;
using FooMap = System.Collections.Generic.Dictionary<char, Foo>;

but C# doesn't seem to like Foo in the second line, even though I've defined it in the first.

Is there a way of using an existing alias as part of another?

Edit: I'm using VS2008

like image 722
quamrana Avatar asked Mar 02 '10 12:03

quamrana


People also ask

What is an alias in C?

Aliasing: Aliasing refers to the situation where the same memory location can be accessed using different names. For Example, if a function takes two pointers A and B which have the same value, then the name A[0] aliases the name B[0] i.e., we say the pointers A and B alias each other.

What is aliasing in variable?

An alias occurs when different variables point directly or indirectly to a single area of storage. Aliasing refers to assumptions made during optimization about which variables can point to or occupy the same storage area.


1 Answers

According to the standard it looks like the answer is no. From Section 16.3.1, paragraph 6:

1 The order in which using-alias-directives are written has no significance, and resolution of the namespace-or-type-name referenced by a using-alias-directive is not affected by the using-alias-directive itself or by other using-directives in the immediately containing compilation unit or namespace body.

2 In other words, the namespace-or-type-name of a using-alias-directive is resolved as if the immediately containing compilation unit or namespace body had no using-directives.

Edit:

I just noticed that the version at the above link is a bit out of date. The text from the corresponding paragraph in the 4th Edition is more detailed, but still prohibits referencing using aliases within others. It also contains an example that makes this explicit.

Depending on your needs for scoping and strength of typing you might be able to get away with something like:

class Foo : System.Collections.Generic.Queue<Bar>
{
}
like image 181
Dave Avatar answered Oct 20 '22 05:10

Dave