Resolving the Conflict of definitions in same namespaces

前端 未结 3 415
醉酒成梦
醉酒成梦 2021-01-26 11:46

I have 2 same named classes defined in 2 different namespaces and I want to include both the namespaces as using statement in my c# file, Like below:

using A.B;
         


        
3条回答
  •  心在旅途
    2021-01-26 12:18

    There is option to create alias for a namespace or a type. This is called a using alias directive:

    using ABFoo = A.B.Foo;
    using CBFoo = C.B.Foo;
    
    if (check)
    {
        ABFoo foo = new ABFoo(); // Use A.B ones class foo
    }
    else
    {
        CBFoo foo = new CBFoo(); // Use C.B ones class foo
    }
    

    or use fully qualified class name:

    if (check)
    {
        A.B.Foo foo = new A.B.Foo(); // Use A.B ones class foo
    }
    else
    {
        C.B.Foo foo = new C.B.Foo(); // Use C.B ones class foo
    }
    

提交回复
热议问题