Home>

c#using three ways to use

1.using instructions.using + namespace name,This allows the types in the command space to be used directly in the program,Without having to specify a verbose namespace of the type,Similar to java import, this function is also the most commonly used,Almost every cs program will be used.

For example:using system;usually appears in * .cs.

2.using alias.using + aliases=specific types that include detailed namespace information.

The advantage of this approach is that when the same cs references two different namespaces,But when both namespaces include a type with the same name.When this type is needed,Everywhere, a detailed namespace is used to distinguish these types of the same name.The alias method is more concise,It is sufficient to make an alias declaration for which class is used.Note:This does not mean that the two names are duplicates,Aliased one of them,The other one does not need to use an alias,If you use both,Both need to use using to define aliases.

using system;
using aclass=namespace1.myclass;
using bclass=namespace2.myclass;
namespace namespace1
{
  public class myclass
  {
    public override string tostring ()
    {
      return "you are in namespace1.myclass";
    }
  }
}
namespace namespace2
{
  class myclass
  {
    public override string tostring ()
    {
      return "you are in namespace2.myclass";
    }
  }
}
namespace testusing
{
  using namespace1;
  using namespace2;
  ///<summary>
  ///Summary description of class1.
  ///</summary>
  class class1
  {
    ///<summary>
    ///The main entry point for the application.
    ///</summary>
    [stathread]
    static void main (string [] args)
    {
      //
      //todo:add code here to launch the application
      //
      aclass my1=new aclass ();
      console.writeline (my1);
      bclass my2=new bclass ();
      console.writeline (my2);
      console.writeline ("press any key");
      console.read ();
    }
  }
}

3.using statement, define a range,Processes the object at the end of the range.

Scenes:

When an instance of a class is used in a snippet,And hope for whatever reason,As long as this code segment is left, the dispose of this class instance is automatically called.

To achieve this,It is also possible to use try ... catch to catch exceptions,But using is also very convenient.

using (class1 cls1=new class1 (), cls2=new class1 ())
{
 //the code using cls1, cls2
} //call the dispose on cls1 and cls2
c
  • Previous Windows nginx installation tutorial and simple practice
  • Next Method for implementing txt popup on page using ajax technology