Search code examples
c#inheritancenamespacesusing

In C# are the `using` directives of the base class inherited by the child class?


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){}
}

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.


Solution

  • 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".

    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.