Ryota Ryota - 22 days ago 8
C# Question

Check using directive reference in Visual Studio

EDIT3: Question rewritten for future reference, thanks to BahJiy

Is there a Visual Studio (or ReSharper) command like "Find Usages" to find class usages from using directive?



Find Usages only shows the reference of the namespace itself (and thus "the only usage" as per below screenshot).

Find Usages on using directive returns "This is the only usage"

When I comment out that

using System.Reactive.Linq
line as per below,
Observable
indicates an error that it doesn't exist in the current context.

enter image description here

I could comment out the using directive and check errors like above, is there a better way than checking errors one by one?




Answer: Find Usages does it, cursor position matters

As mentioned below in the comment to the answer, the cursor position needs to be at
using
, rather than the namespace.

enter image description here







Old question body

Is there a Visual Studio (or ReSharper) command that tells me which methods are coming from
SomeNamespace.SomeClass
defined by
using SomeNamespace.SomeClass
within using file?

I would like to check the use case when I encounter some unexpected namespace.

Find Usages or All References on the using directive only shows other directives.

I could delete the very using directive and check for errors within the file, but I must be ignorant of a better way...



EDIT + EDIT2:

An example may help to clarify the situation better.

SomeClass.cs

namespace SomeNamespace {
public static class SomeClass {
public static string SomeExtensionMethod(this string s){
// some implementation
}
/*
More extension methods
*/
}
}


Consumer.cs

using SomeNamespace.SomeClass

namespace OtherNamespace {
public class Consumer {
public void Consume(){
string id = "someid";
string after = id.SomeExtensionMethod;
}
/*
More extension methods from SomeClass used
*/
}
}


The question is, when I find some
Consumer
having a
using SomeNamespace.SomeClass
, what's the best way to check where the underlying methods are used?

Is there a command similar to Find Usages to get all the methods coming from
SomeNamespace.SomeClass
under
Consumer
class?

Answer

Normally just check your using. Visual Studio only allow a method/class call from one Namespace. If there are two more more method of the same name in multiple of your Namespace that you are using, Visual Studio will force you to specify the Namespace explicative in your call.

i.e.

using Name1;
using Name2;

namespace Class1 {
    class Testing {
        public Testing {
            // this method only exists in Name1
            Method1 ( );
            // this method exist in both Name1 and Name2
            Name1.Method2 ( );
            Name2.Method2 ( );
        }
    }
}

So if you see only

using SomeNamespace;

you will know that any classes that may come from that namespace is specifically from SomeNamespace. If there was another class of that same name but also in a different namespace, Visual Studio will force you to type in that namespace when calling.

So if you see that you usings are normal and there are no random

RandomNamespace.Class1 class1 = new RandomNamespace.Class1 ( );

You know for sure that nothing is out of the ordinary.