C# FAQ: How can type name clashes be resolved
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:FAQs |
| edit |
[edit]
How can type name clashes be resolved?
The C# using keyword can be used to supply hints to the C# compiler regarding the fully qualified names of types residing in a specified source (.cs) file. The using keyword can include an alias which can be used to prevent or resolve name clashes.
Assume the following namespace definitions:
namespace Namespace1 { public class SampleClass {} // same classname as below } namespace Namespace2 { public class SampleClass {} // same classname as above }
Next, the application tries to create an instance of the Namespace2.SampleClass from the following application:
using Namespace1; using Namespace2; public class SampleApplication { public static void Main() { SampleClass sc = new SampleClass (); // Compiler error: type name clash: // Namespace1.SampleClass or Namespace2.SampleClass ? } }
The resulting type name clash can be resolved by creating an alias as follows:
using Namespace1; using SampleClass2 = Namespace2.SampleClass; public class SampleApplication { public static void Main() { SampleClass2 sc = new SampleClass2(); // Creates a new Namespace2.SampleClass. Same as: // Namespace2.SampleClass sc = new Namespace2.SampleClass(); } }
The using keyword can also be used to resolve type name conflicts for types with "System" in their fully qualified names.