Namespace in C #

In the following examples, which one is best used. As you can see, I use only a collection of lists and nothing else from the namespace. Is it true that when executing example1, all classes in the system.collections.generic namespace are loaded into memory or what? How do both examples behave?

Example1: using System.Collections.Generic; public interface ICustomer { List<Customer> GetAll(); } Example2: public interface ICustomer { System.Collections.Generic.List<Customer> GetAll(); } 
+4
source share
8 answers

In the first case, you can declare at the beginning of your class what you are going to use in your class (what are your connections ). Declarations, among other uses, are used by your IDE and compiler to:

  • enable intellisense (autocomplete)
  • activate extension methods (for example, Linq extension methods)

Therefore, without declaring them, you will lose this function. This use is preferred. It also improves code readability. Even if Visual Studio does not recommend that you keep your messages clean (displaying warnings, etc., for example, in Java-based environments), it is always advisable to do this by doing โ€œDelete unused importsโ€ from the context menu to your class file.

The second case allows you to use a type that explicitly refers to it with a full name. This case is usually used only in conflicts with names.

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

+5
source

They are the same; you donโ€™t have to worry about using statements because it mainly affects intellisense performance

Also in case of name collision you can do this:

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

The using statement for importing namespaces is required only for the compiler. Its a shortcut, so you do not need to fully qualify the types that you use.

I prefer to import all namespaces, also if I use only one type in this namespace. To remove an unnecessary namespace, you can use the Visual Studio function to organize namespaces.

+1
source

Both examples behave identically (remember that they are both compiled into intermediate code). I prefer 1, but this is done with clarity, and not with how he behaves.

0
source

The only difference this will make is Intellisense. Having more using statements in the file adds more to what Intellisense needs to index and display. I find the advantage of reading in Example 2 that deserves the extra work of Intellisense.

In addition, both examples are compiled before CIL in the same way and have absolutely no effect on performance.

0
source

AFAIK there is no real benefit to use. The second is more verbose and can make code difficult to read. If you need to reference the list type several times in your interface / class, I would recommend including the appropriate namespace to make it less verbose.

This has been discussed several times on SO before: here and here , for example.

0
source

Depends on usage. If you have many classes with the same name, for example. another class is also called List , the second approach is better (clearer).

Otherwise, I prefer to use approach 1 to get cleaner and formatted codes.

0
source

If you think that you will need to create another list in another function - you can use Example1,

but if it is the only instance of this list, it really does not matter if it is Ex1 or Ex2.

(For order only - I would suggest you use in Ex1)

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

Good - good luck!

0
source

Source: https://habr.com/ru/post/1395174/


All Articles