Search code examples
c#propertiesnamespacessyntax-errorcollision

C#: property/field namespace ambiguities


I get compile error because the compiler thinks Path.Combine refers to my field, but I want it to refer to class System.IO.Path. Is there a good way to handle this other than always having to write the FQN like System.IO.Path.Combine()?

using System.IO;

class Foo
{
   public string Path;

   void Bar(){ Path.Combine("",""); } // compile error here
}

Solution

  • Seperate your references:

    this.Path = System.IO.Path.PathFunction();
    

    I'd strongly suggest implying the System.IO namespace when using Path anywhere inside that class, because it's very difficult to tell the difference. Using the this. qualifier and the full namespace makes them distinct.