The difference between "using MyNameSpace;" and "namespace MyNameSpace"

Hello, I am new to asp.net. I am confused what is the difference between "using MyNameSpace"; and "namespace MyNameSpace". My demo code is the following ...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MyNameSpace ;

namespace MyNameSpace

{
    public partial class DemoPage : System.Web.UI.Page
    {
        My code here
    }
}

In the above code, is there any difference between the two highlighted statements or not. If so, what is it?

Thanks in advance...

+3
source share
3 answers

Yes, they provide additional services.

A using the following directive:

using MyNamespace;

MyNamespace - , MyNamespace.Foo, Foo , , .

: ", , ". MyNamespace.Foo, :

namespace MyNamespace
{
    public class Foo
    {
        ...
    }
}

? using , , - .

+5

using "" . , , , . namespace :


first.cs:

// define the namespace "SomeNamespace"
namespace SomeNamespace
{
    // define a type within the namespace
    class SomeClass { }
}

second.cs:

using SomeNamespace;
// define the namespace "OtherNamespace"
namespace OtherNamespace
{
    class OtherClass
    {
        void SomeMethod()
        {
            // use the type "SomeClass", defined in the "SomeNamespace" namespace
            // note that without the using directive above we would need to write
            // SomeNamespace.SomeClass for this to work.
            SomeClass temp = new SomeClass();
        }
    }
}

temp , using.

0

, . namespace , using , .

using , , .

As you have it using System.Web.UI, the identifier System.Web.UI.Pagecan be written as simple Pageas the compiler knows about the classes in this namespace. If you didn’t have this operator using, you would need the full name for the compiler to find out where to find the class.

0
source

Source: https://habr.com/ru/post/1758678/


All Articles