Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 493
Tim Avatar asked Aug 05 '14 20:08

Tim


People also ask

What does << mean in C?

<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .

What does %d do in C?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

What is the && operator in C?

The && (logical AND) operator indicates whether both operands are true. If both operands have nonzero values, the result has the value 1 . Otherwise, the result has the value 0 . The type of the result is int .


3 Answers

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.

like image 79
David Hoerster Avatar answered Oct 27 '22 13:10

David Hoerster


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.
    }
}
like image 29
Scott Chamberlain Avatar answered Oct 27 '22 13:10

Scott Chamberlain


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.

like image 32
Andrew Avatar answered Oct 27 '22 13:10

Andrew