Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Matthew Stevenson
Matthew Stevenson
4,877 Points

If we are 'using' multiple namespaces which have a Class with the same title, how will the compiler know which to use?

For example, say I am

using System; using someOtherNamespace;

and a class exists in both namespaces called "Console", and in both classes there is a method called "Write" which do slightly different things. When I write the following code, how will the compiler know which namespace to "use"?

Console.Write("Hello");

2 Answers

Steven Parker
Steven Parker
231,261 Points

If your methods have the same signature (number and type of parameters), the compiler will probably complain about an ambiguous reference. You will then have to fully qualify the name using the namespace:

    System.Console.Write("Hello");  // or ....
    someOtherNamespace.Console.Write("Hello");
Todd Anderson
Todd Anderson
4,260 Points

Hi!

Usually the classes and methods will be distinguished by it's parameters.

public void newStr(String a){
     console.print(a);
}

public void newStr(String a, String b){
     console.print(a + b);   
}

These both have the same method name, but the params are different.