Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Where do we use scope Resolution Operator (::) in C#?
The scope resolution operator (::) in C# is used to access types within namespaces, especially when dealing with namespace aliases or when there are naming conflicts. Unlike C++ where it's primarily used for global variables, in C# it specifically relates to namespace resolution.
The scope resolution operator helps distinguish between types that share the same name but exist in different namespaces. It's particularly useful when using namespace aliases or the global keyword.
Syntax
Following is the syntax for using the scope resolution operator with namespace aliases −
alias::TypeName
Following is the syntax for using it with the global namespace −
global::Namespace.TypeName
Using Scope Resolution with Namespace Aliases
When you create a namespace alias, you use the scope resolution operator to access types through that alias −
using System;
using myAlias = System.Collections;
namespace Program {
class Demo {
static void Main() {
myAlias::Hashtable h = new myAlias::Hashtable();
h.Add("M", "1");
h.Add("N", "2");
h.Add("O", "3");
h.Add("P", "4");
foreach (string n in h.Keys) {
Console.WriteLine(n + " " + h[n]);
}
}
}
}
The output of the above code is −
N 2 O 3 M 1 P 4
Using Global Namespace Alias
The global keyword with scope resolution operator ensures you're accessing types from the global namespace, avoiding conflicts with local namespace declarations −
using System;
namespace MyNamespace {
class Console {
public static void WriteLine(string message) {
global::System.Console.WriteLine("Custom Console: " + message);
}
}
class Program {
static void Main() {
Console.WriteLine("Using local Console class");
global::System.Console.WriteLine("Using global System.Console class");
}
}
}
The output of the above code is −
Custom Console: Using local Console class Using global System.Console class
Resolving Namespace Conflicts
When multiple namespaces contain types with the same name, the scope resolution operator helps specify which one to use −
using System;
using WinForms = System.Windows.Forms;
using WebForms = System.Web.UI;
namespace Program {
class Demo {
static void Main() {
// Without scope resolution, this would be ambiguous
// if both namespaces had a Control class
Console.WriteLine("Using WinForms alias: " + typeof(WinForms::Control).FullName);
Console.WriteLine("Using WebForms alias: " + typeof(WebForms::Control).FullName);
// Using global namespace
Console.WriteLine("Global System.String: " + typeof(global::System.String).FullName);
}
}
}
The output of the above code is −
Using WinForms alias: System.Windows.Forms.Control Using WebForms alias: System.Web.UI.Control Global System.String: System.String
Common Use Cases
-
Namespace aliases: Shortening long namespace names for easier access.
-
Avoiding conflicts: When local classes have the same name as system classes.
-
Global access: Ensuring access to global namespace types when local conflicts exist.
-
Code clarity: Making it explicit which namespace a type belongs to.
Conclusion
The scope resolution operator (::) in C# is essential for namespace management and resolving naming conflicts. It works with namespace aliases and the global keyword to provide precise access to types, ensuring your code references the correct classes even when naming conflicts exist.
