0

I'm quite new to C# so I apologize if this has been asked before but I've done some searches and haven't found what I'm looking for.

Specifically, I'm aware I can use the keyword using in the following manner to (in some way) mimic the use of typedef:

using myStringDict = System.Collections.Generic.Dictionary<string, string>;

or other pre-defined types.

I am curious to know if there is a way to do this with generic types. As in

using mySomethingDict<T,K> = System.Collections.Generic.Dictionary<T,K>; //this doesn't work

This is all to avoid having to include using System.Collections.Generic; in my files (as there are many files in my project).

Alternative advice is also welcome.

  • 1
    "as there are many of them" Many usings? Why do you care? You write them once and shouldn´t bother for them at all. Actually the `using` is just to avoid ambioguities, when two classes from different namespaces have the same name but you use them both in your class. Don´t use this keyword as equivalent for `typedef` as it simply *is not*. – MakePeaceGreatAgain Mar 20 '18 at 23:51
  • Your post is muddled *many of them* many what - files or using statements? If you add `using System.Collections.Generic` you can just reference `Dictionary` – Ňɏssa Pøngjǣrdenlarp Mar 20 '18 at 23:53
  • The reason, I was looking to avoid "importing" an entire namespace every time I need an Object (or something else), is because I recall reading it's a bad habit to do this in large projects. Though, I appreciate both your answers. – J. Arrillaga Mar 20 '18 at 23:58
  • Maybe you should add where you read this. As you may see in my answer, this inly refers to languages like JAVA. In C# you can´t import a complete namespace. – MakePeaceGreatAgain Mar 21 '18 at 00:15
  • It's entirely possible I'm mis-remembering as I've been doing a lot of reading on several languages recently. Probably getting things jumbled up in my head. It's good to know C# won't import the entire namespace. – J. Arrillaga Mar 21 '18 at 00:26

2 Answers2

3

No you can't do this, using aliases cannot contain type parameters. From the specification:

9.4.1 Using alias directives

Using aliases can name a closed constructed type, but cannot name an unbound generic type declaration without supplying type arguments. For example:

namespace N1
{
    class A<T>
    {
        class B {}
    }
}
namespace N2
{
    using W = N1.A;         // Error, cannot name unbound generic type
    using X = N1.A.B;           // Error, cannot name unbound generic type
    using Y = N1.A<int>;        // Ok, can name closed constructed type
    using Z<T> = N1.A<T>;   // Error, using alias cannot have type parameters
}
Lee
  • 142,018
  • 20
  • 234
  • 287
2

You´re misusing the using-statement. The meaning of this statement depends on its context. In your case you refer to the "using alias directive" to define an alias for a namespace or a type. However this is usually used to avoid ambuigities, e.g. when you have your own class Math defined but want to use the System.Math-class in your code also. So in order to be able to refer to both types, you can use a using as alias, e.g. using MyMath = MyNamespace.Math.

So using is not the C#-equivalent for a typedef.

On the other side it´s absolutely okay to have multiple usings in your code, it simply shows the classes that are used by your code. You shouldn´t bother for that at all. In contrast to your statement in your question you´re not importing a complete namespace. You simply load the classes you want to use in your code. You could do the exact same by don´t use any using and allways use fully-qualified names for all the types, e.g. System.Generics.Dictionary. This will compile to the exact same code, but is harder to read and write.

This difers from how JAVA imports types - which might be the cause for your confusion. In JAVA you can write import MyNameSpace.* to load all classes within the namespace MyNameSpace. In C# however there´s no such thing, you´re just refering to single types.

See also the examples on the alias

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111