Hello I am new to asp.net. I am confused what is the difference between “using MyNameSpace;” and “namespace MyNameSpace”. My demo code is as follow…
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 yes then what is it?
Thanks in advance…
Yes, they provide complementary services.
A using directive like this:
Tells the compiler to look in the namespace
MyNamespacewhen resolving simple names – so if you have a type calledMyNamespace.Foo, you can just useFooin your source to refer to it when you’ve got this using directive.However, the namespace declaration effectively says, “Anything I declare within this block is in the given namespace”. So to declare the
MyNamespace.Footype, you’d use:Do you see? The
usingdirective says that you want to use things in a particular namespace, whereas the namespace declaration is about putting things into a particular namespace.