Using the advanced use of C #

using keyword has two main purposes:
(a) as an instruction to create or import namespace alias type defined in other namespaces.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Oneclass = one.class1;
using Twoclass = Two.class1;

(B) As the statements to define a range, at the end of this range will release the object.
using statement allows the programmer to specify when objects use resources should release resources. Use objects using statement must implement the IDisposable interface. This interface provides the Dispose method, which will free up resources for this object.
① you can declare object before using statements.
② can declare an object in the using statement.
③ Multiple objects can be used with using statements, but these objects must be declared inside the using statement.

Use rule
①using can only be used to implement the IDisposable interface type, the type prohibited IDisposable interface does not support using statement, or a compile error occurs;
②using statement applies to the case of a single unmanaged resources to clean up, and more unmanaged objects the clean-up is best to try-finnaly to achieve, because there may be nested using statements to hide the Bug. Throws an exception block when using an inner layer, the outer layer will not release the object using the resource blocks;
usage rule
③using initialization statement supports a plurality of variables, provided that these variables must be the same type, for example:

using(Pen p1 = new Pen(Brushes.Black), p2 = new Pen(Brushes.Blue))
      {
          //
      }

④ initialization for a number of different types of variables that can be declared as IDisposable type, for example:

  using (IDisposable font = new Font("Verdana", 12), pen = new Pen(Brushes.Black))
      {
          float size = (font as Font).Size;
          Brush brush = (pen as Pen).Brush;
      }

using the substance
at the assembly stage, the compiler will automatically using statements generated as a try-finally statement, Dispose method and call the object in the finally block to clean up resources. Therefore, using try-finally statement is equivalent to the statement

Regular Expressions
in C # using regular expressions is mainly achieved through the Regex class. Namespace: using System.Text.RegularExpressions.
Which commonly used methods:
Here Insert Picture Description

Please complement deficiencies.

Guess you like

Origin blog.csdn.net/weixin_44370124/article/details/90716713