在C#中,子类继承基类的using指令吗? [英] In C# are the `using` directives of the base class inherited by the child class?
问题描述
假设我们有一个基类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屋!