Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyword global:: when adding reference

I've encountered a strange phenomenon today as I added a reference of a project to another project and trying to use an interface of the referenced assembly:

Situation:

  • The class MySampleApplication is in the project MyApplication.
  • The interface IMySampleApplication is in the referenced project MyApplication.Interfaces.

When I do this:

public class MySampleApplication : IMySampleApplication

Resharper adds the following using:

using global::MyApplication.Interfaces;

Question:

Why is it adding the global:: prefix and how can I avoid this?

Thanks in advance

like image 925
xeraphim Avatar asked Jun 23 '26 15:06

xeraphim


2 Answers

You'll have two occurrences of MyApplication somewhere in your code, eg you'll likely have a class called MyApplication in your local project as well as the namespace in the MyApplication project.

The local MyApplication will take precedence over the namespace in your MyApplication project, so MyApplication.Interfaces will not be found. To get around this, the special alias global:: can be used to tell the compiler that in this case MyApplication refers to a base namespace and so MyApplication.Interfaces can then be correctly identified.

As to how to avoid it, that's easy to say, but it may be harder to achieve: rename one of them to remove the name conflict...

like image 98
David Arno Avatar answered Jun 25 '26 04:06

David Arno


You probably have another entity called MyApplication in your project which is hiding the namespace.

See: "How to: Use the Global Namespace Alias (C# Programming Guide)"

like image 21
Owen Pauling Avatar answered Jun 25 '26 03:06

Owen Pauling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!