Why would anyone fully qualify an object in code when a use case is declared?

I am going through code that has the correct use of the statements declared, but still the objects in the code are fully qualified when they don't need it due to the using statement being declared.

Is there a reason the objects are fully qualified, but there is still a use case declared?

+2


source to share


8 answers


Why do some people (like me) do this on purpose :

When using a relatively rare class, it provides a lot of information about the class. And I like to put information into 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 of the inconsistency and that "when to use" is arbitrary. But for someone reading this code, many questions are answered before they come.

And Intellisense could answer the same questions, but it's not always available.

+2


source


Maybe after the objects were fully qualified a using statement was added? Another less likely cause is namespace conflicts with the objects being used.



+3


source


Sometimes namespace conflicts - there are classes with the same name in multiple namespaces and their full qualifications differentiate them.

+1


source


This is possibly because there are conflicting names in the two imported namespaces.

Let's say A.A

has a type called Foo

( A.A.Foo

) and B.B

has a type called Foo

( B.B.Foo

). If you do this:

using A.A;
using B.B;

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

      

You can 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 just delete the instruction using B.B;

? Suppose you also use types B.B.Bar

, A.A.FooBar

, B.B.Qux

and A.A.Quux

. Then you will want to save the instructions using

.

+1


source


There are many reasons. Some might be:

  • People tend to rely on Intellisense to find the classes they are looking for.
  • The code was pasted from a different location.
  • Bad habits die hard.

A good question is if there is a way that exists in Visual Studio to eliminate this redundancy. I don't know of such a tool (probably something like CodeRush), but maybe someone will comment on one here.

0


source


When you use some of the built-in refactorings, they sometimes fully qualify the object. It's safer to avoid compiler confusion.

0


source


I can think of several reasons:

  • there are multiple using statements nested or included on the same line and there is some ambiguity as to which object a method property belongs to
  • the block used is particularly long and the operator itself can be turned off. The qualification of methods and properties can make it clearer to other programmers.
  • Perhaps there may be very junior programmers in the project who may not fully understand the using statement, and the programmer may try to make it more readable to them.
0


source


One reason this can happen: auto-generated code. (e.g. code generated by Windows Forms Designer)

0


source







All Articles