Resolving the Conflict of definitions in same namespaces

前端 未结 3 410
醉酒成梦
醉酒成梦 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:13

    Suppose, you have a class Foo in NameSpaceA

    namespace NameSpaceA
    {
        class Foo
        {
            public string Sample { get; set; }
        }
    }
    

    and you have also class Foo in NameSpaceB

    namespace NameSpaceA
    {
        class Foo
        {
            public string Sample { get; set; }
        }
    }
    

    Now you can use this class in main method like this

    using AFoo = NameSpaceA.Foo;
    using BFoo = NameSpaceB.Foo;
    if(check)
    {
       AFoo afo = new AFoo();
    }
    else
    {
       BFoo bFoo = new BFoo();
    }
    

    It is called aliasing Aliasing

    You can also use this way. It is called fully qualified name link

    if(check)
    {
       NameSpaceA.Foo afo = new NameSpaceA.Foo();
    }
    else
    {
       NameSpaceB.Foo bFoo = new NameSpaceB.Foo();
    }
    

    If this helps you please select this as a correct answer. It will help the community.

提交回复
热议问题