4

In following examples, which one is the best use. As you see I am using only list collection and nothing else from the namespace. Is it like this that when Example1 is executed all the classes under namespace system.collections.generic are loaded in memory or what? How both examples behave?

Example1:

using System.Collections.Generic;
public interface ICustomer
{
    List<Customer> GetAll();
}


Example2: 

public interface ICustomer
{
    System.Collections.Generic.List<Customer> GetAll();
}
mrd
  • 2,095
  • 6
  • 23
  • 48

8 Answers8

5

The first case let's you declare at the beginning of your class what you are intending to use in your class (what are your couplings). Declarations, among other uses, are used by your IDE and compiler to:

  • enable intellisense (autocompletion)
  • activate extension methods (e.g. Linq extension methods)

So without declaring them you will loose this feature. This use is preferrable. It improves code readability too. Even if Visual Studio does not strongly encourages you to keep your usings clean (displaying warnings and so on like, for example, in java based environments), it is always preferrable to do this doing a "Remove unused imports" from the context menu on your class file.

The second case lets you use a type explicitly referencing it by fully qualified name. This case is normally used only in name conflict situations.

In the end, binary compiled code will be the same because compiler will optimize it, but the compile process itself could be more efficient if you use the first approach.

Andrea Colleoni
  • 5,919
  • 3
  • 30
  • 49
1

They are the same; you shouldn't be worried about using statements because it mostly affects the intellisense performance

Also in case of name collisions you can do this:

using Generic = System.Collections.Generic;
public interface ICustomer
{
    Generic.List<Customer> GetAll();
}
fardjad
  • 20,031
  • 6
  • 53
  • 68
  • 1
    In my opinion, the only use for this is when you have name collisions and don't want to write out the entire namespace for one of them. Other than that it clutters up code and makes it harder to read. – Robert Rouhani Feb 07 '12 at 08:11
  • Indeed now you have to know Generic stands for the System.Collections.Generic. If I see this I think the Generic.List stands for something custom made than .net libraries. – RvdK Feb 07 '12 at 08:32
  • Just curious why the answer got a downvote! if there's something wrong with it, let me know and I will delete the answer! – fardjad Feb 07 '12 at 08:37
  • @PoweRoy so what else you would do in case of name collisions? You should either use FQNs or aliases. – fardjad Feb 07 '12 at 08:38
  • @fardjad: Name collisions only occur with custom made stuff. So I would alias the custom stuff and leave the rest alone. (Note the -1 is not from me) – RvdK Feb 07 '12 at 08:56
1

The using statement to import namespaces is only necessary for the compiler. Its a shortcut, so that you don´t need to fully qualify the types you are using.

I prefer to import all namespaces, also if i only use one type in that namespace. To remove unneeded namespace you could use the function of Visual Studio to organize namespaces.

Jehof
  • 34,674
  • 10
  • 123
  • 155
0

Both examples behave exactly the same (remember that they are both compiled to intermediate code anyway). I prefer 1, but that's to do with clarity, not how it behaves.

Aidan
  • 4,783
  • 5
  • 34
  • 58
0

The only difference it will make is with Intellisense. Having more using statements in a file add more to what Intellisense has to index and display. I find the benefit of Example 2's readability worth the extra work Intellisense does.

Other than that, both examples get compiled down to CIL in the same way and it has absolutely no effect on performance whatsoever.

Robert Rouhani
  • 14,512
  • 6
  • 44
  • 59
0

AFAIK there's no real benefit in using either. The second one is just more verbose and might make reading through the code more difficult. If you have to reference the List type multiple times in your interface / class, I'd recommend including the relevant namespace to make it less verbose.

This has been discussed a couple of times on SO before though: here and here, for example.

Community
  • 1
  • 1
FarligOpptreden
  • 5,013
  • 22
  • 23
0

Depends on usage. If you have many classes with same name, e.g. have another class also called List, 2nd approach is better ( clearer ).

Otherwise, I prefer to use approach 1 to have cleaner & formatted codes.

Raptor
  • 53,206
  • 45
  • 230
  • 366
0

If you think you will need to create one more list in another function - you sould use Example1,

but If this is the only instance of this list - it doesn't really matter if it is Ex1 or Ex2.

(Just for the order - I'd offer you to use in Ex1)

And - no, when you use Using - not all the library is loaded... What is loaded - is just, for example, the List that you call to.

Good - Luck !

ParPar
  • 7,355
  • 7
  • 43
  • 56