简体   繁体   中英

why would someone fully qualify an Object in code when there is a using statement declared?

I am looking at code which has the correct using statements declared but yet the objects within the code are fully qualified when they don't need to be due to the fact that the using statement is declared.

Is there a reason why the objects are fully qualified but yet still have the using statement declared?

Perhaps the using statement was added after the objects were fully qualified? Another less likely reason is that there are namespace conflicts with the objects in use.

Why some people (like me) do it intentionally :

When using a relatively rare class, it provides a lot of information about the class. And I like puting information in the code. Consider:

System.Runtime.Serialization.Formatters.Soap.SoapFormatter formatter = 
     new SoapFormatter();  // .NET 2

or

var formatter = new  // .NET 3
   System.Runtime.Serialization.Formatters.Soap.SoapFormatter.SoapFormatter();  

I am fully aware about the inconsistency and that the 'when to use' is kind of arbitrary. But for somebody reading this code a lot of questions are answered before they come up.

And Intellisense could answer the same questions but it is not always available.

有时,命名空间会发生冲突-多个命名空间中存在相同名称的类,并且完全限定它们的作用即可将它们区分开。

It may be because there are conflicting names within two imported namespaces.

Say, AA has a type called Foo ( AAFoo ), and BB has a type called Foo ( BBFoo ). If you do this:

using A.A;
using B.B;

// class definitions... etc
    var x = new Foo(); // which foo?

You could do this if you don't want to fully qualify it:

using A.A;
using B.B;
using AFoo = A.A.Foo;
using BFoo = B.B.Foo;

// class definitions... etc
    var x = new AFoo();

Why not simply remove the using BB; statement? Well, supposed you're also using types BBBar , AAFooBar , BBQux and AAQuux . You would want to keep the using statements then.

There are lots of reasons. A few might be:

  1. People tend to lean on Intellisense to find the classes they are looking for.
  2. Code has been pasted from elsewhere.
  3. Bad habits die hard.

A good question is whether there is a way that exists in Visual Studio to eliminate this redundancy. I am not aware of such a tool (probably something like CodeRush), but perhaps someone will comment here with one.

When you use some of the built in refactorings they sometimes fully qualify the object. This is safer to avoid compiler confusion.

I can think of a few reasons:

  1. there are multiple using statements, either nested or included on the same line, and there is some ambiguity as to which object the method property belongs to
  2. the using block is particularly long and the statement itself might be off screen. Qualifying the methods and properties can make it more readable for other programmers.
  3. there might be some very junior programmers on the project who might not quite understand the using statement and the programmer might be trying to make it more readable for them.

One reason this can happen: auto-generated code. (like the Windows Forms designer-generated code)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM