This afternoon, I checked the C #3.0 coding specification and found some obscure codes that I did not understand. These codes are sometimes useful.
Class @ class {public static void @ static (bool @ bool) {if (@ bool) System. console. writeLine ("true"); else System. console. writeLine ("false ");}}
@ Indicates that if you cannot avoid using the C # keyword, add @ before the keyword. No error will be reported during code compilation.
Lock
{
}
Unlock
{
}
Lock and unlock are generally used in multiple threads to lock and unlock a code segment to prevent two threads from simultaneously using a code segment.
Check
{
}
Uncheck
{
}
Check and uncheck are used to check whether the data type in the code segment has exceeded the boundary. check and uncheck are not checked. When uncheck exceeds the boundary, no exception is triggered. the minimum value of the Data Type mixValue is automatically returned.
Enumerator in yield return C #
With C #2.0, these problems have melted like snow at the end of May. In version 2.0 of this example, I overwrite the list above and use two new features of C #2.0: generics and enumerators.
I will redefine the ListBox to implement IEumerable <string> as the start:
Public class ListBox: IEnumerable <string>
In this way, make sure that this class can be used in the foreach loop and that the iteration value is of the string type.
Now, the entire nested class is removed from the previous example and the GetEnumerator method is replaced with the following code.
Public IEnumerator <string> GetEnumerator ()
{
Foreach (string s in strings)
{
Yield return s;
}
}
The GetEnumerator method uses the new yield statement. The yield statement returns an expression. The yield statement only appears in the iteration block and returns the expected value of the foreach statement. That is, each call to GetEnumerator will generate the Next string in the set. All status management is done for you!
You have done that. You do not need to implement your own enumerator for each type, and you do not need to create Nested classes. You have removed at least 30 lines of code and greatly simplified your code. The program continues to run as expected, but state management is no longer your task, and everything is done for you. Furthermore, the value returned by the enumerator must be of the string type. If you want to return other types, you can modify the IEnumerable generic statement. The IEnumerable generic statement will reflect the new type.
More about Yield
It is worth noting that you can add more than one yield value in the yield statement block. In this way, the following statements are completely correct C # statements:
Public IEnumerator GetEnumerator ()
{
Yield return "Who ";
Yield return "is ";
Yield return "John Galt? ";
}
If the above Code is located in a class named foo, you can write it like this:
Foreach (string s in new foo)
{
Console. Write (s );
}
The output result will be:
Who is John Galt?
If you stop thinking, this is what the previous Code did. It traverses its own foreach loop and generates each string it finds.