Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Statement with Generics: using ISet<> = System.Collections.Generic.ISet<>

Since I am using two different generic collection namespaces (System.Collections.Generic and Iesi.Collections.Generic), I have conflicts. In other parts of the project, I am using both the nunit and mstest framework, but qualify that when I call Assert I want to use the nunit version by

using Assert = NUnit.Framework.Assert; 

Which works great, but I want to do the same thing with generic types. However, the following lines do not work

using ISet = System.Collections.Generic.ISet; using ISet<> = System.Collections.Generic.ISet<>; 

Does anyone know how to tell .net how to use the using statement with generics?

like image 967
Jason More Avatar asked Sep 15 '10 17:09

Jason More


People also ask

What is using System collections generic?

System.Collections.Generic ClassesIt stores key/value pairs and provides functionality similar to that found in the non-generic Hashtable class. It is a dynamic array that provides functionality similar to that found in the non-generic ArrayList class.

What are generics difference between generics and collection?

Generics is a programming tool to make class-independent tools, that are translated at compile time to class-specific ones. Collections is a set of tools that implement collections, like list and so on. In C# 3 and above you have generic collections, that are template based - thus you have generic collections.

What is the use of using System collections generic in C#?

Contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.

How do you write a generic class in C#?

You can create an instance of generic classes by specifying an actual type in angle brackets. The following creates an instance of the generic class DataStore . DataStore<string> store = new DataStore<string>(); Above, we specified the string type in the angle brackets while creating an instance.


2 Answers

Unfortunately, the using directive does not do what you want. You can say:

using Frob = System.String; 

and

using ListOfInts = System.Collections.Generic.List<System.Int32>; 

but you cannot say

using Blob<T> = System.Collections.Generic.List<T> 

or

using Blob = System.Collections.Generic.List 

It's a shortcoming of the language that has never been rectified.

like image 184
Eric Lippert Avatar answered Sep 17 '22 09:09

Eric Lippert


I think you're better off aliasing the namespaces themselves as opposed to the generic types (which I don't think is possible).

So for instance:

using S = System.Collections.Generic; using I = Iesi.Collections.Generic; 

Then for a BCL ISet<int>, for example:

S.ISet<int> integers = new S.HashSet<int>(); 
like image 29
Dan Tao Avatar answered Sep 18 '22 09:09

Dan Tao