Use static type instead of variable

Is there a way to tell the compiler to use a static type instead of a variable if your project does not use namespaces?

For example, I have a User class with various static and non-stationary methods. Let's say one of the static methods is called GetUser() .

I am trying to call this User.GetUser() method from a method that also has a variable in an area (inherited from the base class) called User. However, the compiler complains that it cannot find User.GetUser() because it believes that I mean a user variable that is in scope.

If this project used namespaces, I could just do ns1.User.GetUser() , but in this case this is not possible. Is there a way I can tell the compiler that I mean the type User instead of the variable User ?

+6
source share
4 answers

You can use:

 global::User.GetUser() 

Or use the directive for an alias like:

 using UserType = User; ... UserType.GetUser(); 

I highly recommend you use namespaces :)

+9
source

Can you write global::User.GetUser() ?

See global

+6
source
  • Use global::User.GetUser() .

  • Use an alias: using UserClass = User;

  • Rename the variable.

  • Rename the type.

  • Reduce the scope of the variable so that it is no longer in the area where you use it.

+4
source

Alternatively, you can use an alias for your static class. In your using directives you can add:

 using StaticUser = User; 

Then there will be no more ambiguity.

+2
source

All Articles