在C#中,子类继承基类的using指令吗? [英] In C# are the `using` directives of the base class inherited by the child class?

查看:88
本文介绍了在C#中,子类继承基类的using指令吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let's say we have a base class Rectangle and a derived class 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){}
}

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

Does the Square class have to explicitly say that it is using System.Foo? I'm getting erratic results. In one project the using directives seem to be inherited and in a web application they aren't.

推荐答案

using语句不编译为代码-它们有助于使您的代码阅读得更干净.结果,它们不是继承"的.

using statements, in this context, don't compile to code -- they are helpers to make your code read cleaner for others. As a result, they are not "inherited".

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

So, to answer your question, your Square class needs to reference System.Foo - either with a using statement, or by using a fully qualified class name.

这篇关于在C#中,子类继承基类的using指令吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆