10

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.

Tobbe
  • 1,825
  • 3
  • 21
  • 37
Tim
  • 8,669
  • 31
  • 105
  • 183

5 Answers5

12

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.

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
10

A using statement will only propagate to the next set of closing braces (}) from the level it was declared on within the same the file.

//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.
    }
}
//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.
    }
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • My question was whether the imports became part of the base class. I'm not sure what `propagate` means in the context of class inheritance. – Tim Aug 05 '14 at 20:47
  • [Propagate](http://www.merriam-webster.com/dictionary/propagate): "*to pass along to offspring*". A using statement in a base class will not propagate (be passed along to offspring) child classes. – Scott Chamberlain Aug 05 '14 at 21:50
  • Thanks for following up. You said "propagate to the next set of closing braces" and they are not "offspring", so "propagate" is being used to mean more than one thing. – Tim Aug 05 '14 at 23:11
  • @Tim [using directives](https://msdn.microsoft.com/en-us/library/sf0df423.aspx) do not become part of the class, they are part of the code file itself. The compiler (msbuild.exe), and visual studio intellisense use them to resolve type namespaces when compiling your code files. – Zack Sep 02 '15 at 15:01
2

using directives are only shared if the classes are in the same file and they are not nested in the classes themselves like in your example.

For instance:

using System.Foo;
namespace N
{
    class A {}
    class B {}
}

If this is all in one file, A and B can both use Foo.

Andrew
  • 4,953
  • 15
  • 40
  • 58
  • I think "shouldn't" is an overly-strong statement. There are a few corner cases where there's a difference, but it's *mostly* a matter of preference. See http://blogs.msdn.com/b/ericlippert/archive/2007/06/25/inside-or-outside.aspx – Jon Skeet Aug 05 '14 at 20:16
  • @JonSkeet Thanks for the article. I have removed my claim. – Andrew Aug 05 '14 at 20:18
2

I think everyone is missing the point when it comes to using directives. They really have nothing to do with the class at all. using directives in a code file (.cs, .vb, etc...) are not part of the classes defined within the file. They are used by the compiler to resolve namespaces when compiling.

Zack
  • 2,789
  • 33
  • 60
1
using System.Foo;

namespace  Shapes {...

Importing should always be top most and not within a namespace. This will allow the entire structure of the class to rely on that import when needed.

  • Not clear how their being after the namespace declaration makes the imports not accessible to the `class` in its entirey. – Tim Aug 05 '14 at 20:43