Three USES of using in c sharp

  • 2020-05-05 11:45:24
  • OfStack

The using   directive has two USES:
Types are allowed in a namespace so that you do not have to restrict the types used in that namespace.
Create an alias for the namespace.
The using   keyword is also used to create the   using   statement     defines a scope outside which one or more objects will be released.
See   using   statement. http: / / www. yaosansi. com/blog/article asp? id
= 669 using   namespace;
using   alias   =   type|namespace;  
Parameter
Alias  
The user-defined symbol that you want to use to represent a namespace or type. You can then use   alias   to represent namespace names.
Type  
The type you want to represent by   alias  .
namespace  
The namespace that you want to represent by   alias  . Or a namespace that contains the type you want to use without specifying a fully qualified name.
Note
The scope of the using   directive is limited to the file that contains it.  
Create an   using   alias to make it easier to qualify an identifier to a namespace or type.
Create the   using   directive to use a type in a namespace without specifying a namespace. The using   directive does not give you access to any namespace nested within the specified namespace.
There are two types of namespaces: user-defined namespaces and system-defined namespaces. A user-defined namespace is a namespace defined in the code. For a list of the namespaces defined by the system, see  .NET   Framework   class library reference.
For an example of referencing methods in other assemblies, see creating and using   C#   DLL.
Example   1
Explain
The following example shows how to define and use the   using   alias:
Code
                            using   MyAlias   =   MyCompany.Proj.Nested;
//   Define   an   alias   to   represent   a   namespace.
namespace   MyCompany.Proj
{
public   class   MyClass
{
public   static   void   DoNothing()
{
}
}
} example   2
Explain
The following example shows how to define the   using   directive and   using   alias:
Code
//   cs_using_directive2.cs
//   Using   directive.
using   System;
//   Using   alias   for   a   class.
using   AliasToMyClass   =   NameSpace1.MyClass;
namespace   NameSpace1
{
public   class   MyClass
{
public   override   string   ToString()
{
return   "You   are   in   NameSpace1.MyClass";
}
}
}
namespace   NameSpace2
{
class   MyClass
{
}
}
namespace   NameSpace3
{
//   Using   directive:
using   NameSpace1;
//   Using   directive:
using   NameSpace2;
class   MainClass
{
static   void   Main()
{
AliasToMyClass   somevar   =   new   AliasToMyClass();
Console.WriteLine(somevar);
}
}
} output
You   are   in   NameSpace1.MyClass

Related articles: