C# using alias as type parameter in other using alias

Viewed 2695

I'm trying to define a pair of type aliases at the top of my C# program. This is a short example of what I'm trying to do:

using System;
using System.Collections.Generic;

namespace Foo {
    using TsvEntry = Dictionary<string, string>;
    using Tsv = List<TsvEntry>;
}

When I try to compile this using mcs 3.2.8.0, I get the following error message:

foo.cs(6,19): error CS0246: The type or namespace name `TsvEntry' could not be found. Are you missing an assembly reference?

Is it possible to use using aliases within other aliases in C#, or am I missing something about the way using statements work?

6 Answers

(Adding this here because I ran across this question a lot when searching for a related issue, so it might help others)

If you're looking to use aliases to functions for a functional style of programming, what you're looking for is the delegate keyword. You can think of delegate as making an interface, except it's for a function.

so instead of using Expression = Func<string>; using Converter = Func<List<string>, Expression>; you do delegate string Expression(); delegate Expression Converter(List<string> tokenizedExpressions);

https://weblogs.asp.net/dixin/functional-csharp-function-type-and-delegate

I agree with the advice to avoid adding nested namespaces just to allow for reuse of aliases. Note however that you get one level of nesting for free. Define your basic aliases outside your own namespace. Reuse the basic aliases to define aliases for compound types within your namespace.

Related