[C #6] 1-using static,

Source: Internet
Author: User
Tags mscorlib

[C #6] 1-using static,
0. Directory

C #6 Add feature catalog

1. Old Version code
 1 using System; 2  3 namespace csharp6 4 { 5     internal class Program 6     { 7         private static void Main(string[] args) 8         { 9             Console.WriteLine("blackheart");10         }11     }12 }

The above code is not familiar to anyone. Use the static method WriteLine of the static Console class to output a line of strings. The plug-in point has knowledge about CLR. Does CLR have a namespace concept during the execution of IL, all it knows is the fully qualified name of the member (C #1 basis ). That is, when you call Console. WriteLine, the complete type name System. Console. WriteLine is used in IL. Open ILDASM to see the generated IL Code as follows:

 1 .method private hidebysig static void  Main(string[] args) cil managed 2 { 3   .entrypoint 4   // Code size       13 (0xd) 5   .maxstack  8 6   IL_0000:  nop 7   IL_0001:  ldstr      "blackheart" 8   IL_0006:  call       void [mscorlib]System.Console::WriteLine(string) 9   IL_000b:  nop10   IL_000c:  ret11 } // end of method Program::Main

Therefore, when we need to call many methods of the Console, We need to write the Console prefix many times. Can we simplify it?

2. C #6 new using static syntax
 1 using static System.Console; 2  3 namespace csharp6 4 { 5     internal class Program 6     { 7         private static void Main(string[] args) 8         { 9             WriteLine("blackheart");10         }11     }12 }

The important part is in the first line using static System. Console;, we use "using static" to reference a static type, System. Console,In the current global scope, any static member that calls the Console can omit the Console. This type name prefix. Does it look refreshing! So what's the wonder of Its Compiler? Let's use ILDASM to check IL and use IL to find out the details:

 1 .method private hidebysig static void  Main(string[] args) cil managed 2 { 3   .entrypoint 4   // Code size       13 (0xd) 5   .maxstack  8 6   IL_0000:  nop 7   IL_0001:  ldstr      "blackheart" 8   IL_0006:  call       void [mscorlib]System.Console::WriteLine(string) 9   IL_000b:  nop10   IL_000c:  ret11 } // end of method Program::Main

It is exactly the same as the result of the old syntax compilation, and, so, its essence is just a syntactic sugar.

2.1 can I use the using static non-static type?

The answer is yes.But you can only use its static method. As for the reason, do not explain it.

1 using static csharp6.MyClass; 2 3 namespace csharp6 4 {5 public class MyClass 6 {7 public void MyFunction (string value) 8 {9} 10 11 public static void MyStaticFunction (string value) 12 {13} 14} 15 16 internal class Program17 {18 private static void Main (string [] args) 19 {20 // 21 MyFunction ("blackheart") not allowed "); 22 // 23 MyStaticFunction ("blackheart"); 24} 25} 26}
2.2 What other types can I use using static?

Besides classStruct,EnumType:

1 using static csharp6.MyStruct; 2 using static System. consoleColor; 3 4 namespace csharp6 5 {6 public struct MyStruct 7 {8 public static void MyStructStaticFunction (string value) 9 {10} 11} 12 13 internal class Program14 {15 private static void Main (string [] args) 16 {17 // static function of the structure type 18 MyStructStaticFunction ("blackheart"); 19 // enumerate ConsoleColor. black20 System. console. foregroundColor = Black; 21} 22} 23}
2.3 What should I do if the using staitc member signature is the same as the existing static member signature?

High priority of existing static members, Prefer to use existing static members. For example:

1 using static System. console; 2 3 namespace csharp6 4 {5 internal class Program 6 {7 private static void Main (string [] args) 8 {9 // The non-using static import method with the same signature has a high priority of 10 // so we will call our own declared WriteLine and output "My priority is high!" 11 WriteLine ("blackheart"); 12} 13 14 // WriteLine method with the same signature 15 private static void WriteLine (string value) 16 {17 System. console. writeLine ("My priority is relatively high! "); 18} 19} 20}
2.4 What should I do if the signatures are the same between using static members?

Let's take a look at the following code:

 1 using static csharp6.MyClass; 2 using static System.Console; 3  4 namespace csharp6 5 { 6     public static class MyClass 7     { 8         public static void WriteLine(string value) 9         {10         }11     }12 13     internal class Program14     {15         private static void Main(string[] args)16         {17             WriteLine("blackheart");18             ReadKey();19         }20     }21 }

We use two types of using static. Both types have a WriteLine static method with the same method signature. How can the compiler handle this?

The compiler reports an error., CS0121, tells us that there is an ambiguity when calling WriteLine, and it cannot tell which type we want to use. So,We need to remove a using static statement or use the old version..

2.5 What types of members can be used in addition to methods?

Static attributes, fields, events, etc. static members can use using static to omit the type prefix.

1 using static csharp6.MyClass; 2 3 namespace csharp6 4 {5 class MyClass 6 {7 public static void Method () {} 8 public static int Field; 9 public static int Property {get; set;} 10 public static event System. action Event; 11} 12 internal class Program13 {14 private static void Main (string [] args) 15 {16 Method (); // static Method 17 var field = Field; // static field 18 var property = Property; // static attribute 19 Event + = Program_Event; // static Event 20 21} 22 23 private static void Program_Event () 24 {25 throw new System. notImplementedException (); 26} 27} 28}
3. using static Extension Method

Since the using static method can be applied to the static method, can we use the extension method we are familiar with in C #3?The answer is in a specific syntax format.(The first parameter of the extension method must be written according to the call method of the instance method.), I cannot understand that the implementation of the extension method is a static method, but the first parameter is a special this parameter. Why can I directly use a fully qualified name of the type, but what if using is static and cannot be used? After compilation, It will be translated into calls with fully qualified names. I really don't understand. As follows:

1 using static System. linq. enumerable; 2 3 namespace csharp6 4 {5 internal class Program 6 {7 private static void Main () 8 {9 // allowed, which is originally a non-extension method. 10 var range = Range (5, 17); 11 // not allowed, do not understand the location 12 // var odd = Where (range, I => I % 2 = 1); // 13 is not allowed. // according to the principle, the above Code will be translated as this, I really don't understand why the above method is not allowed 14 var odd = System. linq. enumerable. where (range, I => I % 2 = 1); 15 // yes, this compilation effect is exactly the same as that of the odd above. 16 var odd2 = range. where (I => I % 2 = 1); 17} 18} 19}
4. Summary

This blog post introduces a new syntax feature of C #6. The using static syntax imports a type, and then it can be within its global scope (within the current file) you can use it to access static members (subject to the limitation of the access modifier). Pay attention to the following points:

Finally, the most important thing is that using static is only the syntactic sugar of the compiler, which helps us simplify the Code. There is no essential difference with the previous writing method, and there is no difference after compilation..

5. Reference

C # Language Reference-using command: Using Static Type

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.