using keyword in C#

The using keywords has three major uses:

  The using statement defines a scope at the end of which an object will be disposed.

  The using directive creates an alias for a namespace or imports types defined in other namespaces.

  The using static directive imports the members of a single class.

 

1.using statement defines a scope, the scope will eventually dispose of the object.

 [Note] dispose emphasis on the "clean-up resources" to clean up after the completion of the resource, the object itself does not release memory. The so-called "dispose of an object" or "close to an object," the true meaning: clean up the object in a package of resources (such as his field referenced objects), and then wait for the garbage collector automatically reclaims memory occupied by the object itself.

2.using instruction has two roles, one imported namespace, which is its most common usage; the second is to create an alias (alias) for the namespace

Alias ​​has two effects: the elimination of two types of the same name ambiguity; abbreviated long name

 1 using System;
 2 using System.Threading;
 3 using CountDownTimer = System.Timers.Timer;
 4 namespace UsingDemo
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             // CountDownTimer is alias of System.Timers.Timer,
11             //declaring with using directive
12 
13             CountDownTimer timer;
14         }
15     }
16 }

Timer System.Timers.Timer used as an alias, to use System.Threading.Timer type, must be fully qualified, or define a new alias.

 1 using System;
 2 using System.Threading;
 3 using Timer = System.Timers.Timer;
 4 namespace UsingDemo
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             Timer timer;
11             System.Threading.Timer timer1;
12         }
13     }
14 }

 

3.using static command

Is on using static instructions and using essentially the same instruction, are introduced into the namespace syntactic sugar introduced in C # 6.0, For details, see:

https://www.cnblogs.com/linianhui/p/csharp6_using-static.html

 

Guess you like

Origin www.cnblogs.com/zps-blog/p/12571769.html