在C#中是子类继承的基类的`using`指令?

Tim*_*Tim 9 c# inheritance namespaces using

假设我们有一个基类Rectangle和一个派生类Square:

namespace Shapes {
    using System.Foo;

    public class Rectangle {
        public Rectangle(int l, int w){}
    }
}

namespace Shapes {
   public class Square : Rectangle

   public Square(int l, int w){}
}
Run Code Online (Sandbox Code Playgroud)

Square课程是否必须明确说它正在使用System.Foo?我的结果变得不稳定了.在一个项目中,using指令似乎是继承的,而在Web应用程序中则不是.

Dav*_*ter 11

using在这种情况下,语句不会编译为代码 - 它们是帮助您使代码更清晰的帮助程序.结果,它们不是"继承"的.

因此,要回答您的问题,您的Square类需要引用System.Foo- 使用using语句或使用完全限定的类名.


Sco*_*ain 9

一个using语句只会传播到下一组结束括号(的}从水平它被宣布在同一个文件内).

//From File1.cs
using System.Baz;
namespace Example
{
    using System.Foo;
    //The using statement for Foo and Baz will be in effect here.

    partial class Bar
    {
        //The using statement for Foo and Baz will be in effect here.
    }
}

namespace Example
{
    //The using statement for Baz will be in effect here but Foo will not.

    partial class Bar
    {
        //The using statement for Baz will be in effect here but Foo will not.
    }
}
Run Code Online (Sandbox Code Playgroud)
//From File2.cs
namespace Example
{
    //The using statement for Foo and Baz will NOT be in effect here.
    partial class Bar
    {
        //The using statement for Foo and Baz will NOT be in effect here.
    }
}
Run Code Online (Sandbox Code Playgroud)