C #4.0 syntactic sugar Article 4: Extension Method,
Today, I will continue to share the C #4.0 syntax sugar extension method, which is my favorite method. Let's first think about what we will do if the original type we previously wrote cannot meet our current needs, but we need to add new methods to this type to implement the current situation. Let me first explain how I did not learn this knowledge point:
The most stupid way is to modify the original type, and then add a method to achieve the required change, as shown in the following code:
1 public class KuozFF 2 3 {4 5 public void NormalMethod () 6 7 {8 9 Console. writeLine ("I am the original method"); 10 11} 12 13 public void ExtensionMethod () 14 15 {16 17 Console. writeLine ("I'm an extension method"); 18 19} 20 21}View Code
Call method:
1 KuozFF method = new KuozFF (); 2 3 method. NormalMethod (); 4 5 method. ExtensionMethod (); 6 7 Console. ReadLine ();View Code
The output result is as follows:
For example, if someone else gives you a dll file, you cannot modify it. But what if you want to add your method to this class?
Write a class by yourself and the class inherits from the original class. The Code is as follows:
1 public class KuozFF 2 3 {4 5 public void NormalMethod () 6 7 {8 9 Console. writeLine ("I am the original method"); 10 11} 12 13 14 15} 16 17 18 19 public class MYKZFF: KuozFF20 21 {22 23 public void ExtensionMethod () 24 25 {26 27 Console. writeLine ("I'm an extension method"); 28 29} 30 31}View Code
The call code is as follows:
1 MYKZFF method = new MYKZFF (); 2 3 method. NormalMethod (); 4 5 method. ExtensionMethod (); 6 7 Console. ReadLine ();View Code
The effect is as follows:
The above results show that the effect is the same, but some people do not want to write the inheritance class or modify the source code. What should I do? At this time, the extension method was born! Let's take a look at the official explanation:
Extension Method: allows you to "add" methods to an existing type without creating a new derived type, re-compiling, or modifying the original type in other ways. An extension method is a special static method, but it can be called like an instance method of an extension type. For client code written in C #, there is no significant difference between calling extension methods and calling methods actually defined in the type.
This is the explanation on Microsoft MSN. Let's look at the code and what the extension method looks like:
1 public class KuozFF 2 3 {4 5 public void NormalMethod () 6 7 {8 9 Console. writeLine ("I am the original method"); 10 11} 12 13 14 15} 16 17 18 19 public static class ExtensionClass20 21 {22 23 public static void ExtensionMethod (this KuozFF k) 24 25 {26 27 Console. writeLine ("I'm an extension method"); 28 29} 30 31}View Code
The call code is as follows:
1 KuozFF method = new KuozFF (); 2 3 method. NormalMethod (); 4 5 method. ExtensionMethod (); 6 7 Console. ReadLine ();View Code
Output result:
From the code above, we can see that we do not need to worry about where the extension method is written when calling the client. You only need to instantiate the original class and the extension method will automatically have it.
Extension methods are everywhere in C #4.0. Let's take a look at the built-in extension methods in C # For a deeper understanding:
1 public static class Enumerable 2 3 {4 5 public static IEnumerable <TSource> Where <TSource> (this IEnumerable <TSource> source, Func <TSource, bool> predicate ); 6 7 public static IEnumerable <TSource> Where <TSource> (this IEnumerable <TSource> source, Func <TSource, int, bool> predicate ); 8 9 public static IEnumerable <TResult> Select <TSource, TResult> (this IEnumerable <TSource> source, Func <TSource, TResult> selector ); 10 11 public static IEnumerable <TResult> Select <TSource, TResult> (this IEnumerable <TSource> source, Func <TSource, int, TResult> selector); 12 13}View Code
The above is the extension method of the Microsoft IEnumerable class, so we usually use the method after the dot. Then we can see how many rich where, select and other methods are the extension method. Here, I just came up with a method for extension.
Next we will write down the string class extension method. We used to judge whether a string is null or empty, and used the built-in system method string. isNullOrEmpty (s:
1 public static class Demo1 2 3 {4 5 public static bool IsNullOrEmpty (this string s) 6 7 {8 9 return string. IsNullOrEmpty (s); 10 11} 12 13}View Code
The call code is as follows:
1 string temp = "12"; 2 3 bool result = temp. IsNullOrEmpty (); 4 5 Console. WriteLine (result); 6 7 Console. ReadLine ();View Code
Output result:
From the call code, we can see that the string itself does not have the IsNullOrEmpty () method. We can use this method to write the extension method by ourselves.
You can write your own methods or extensions to the system class to facilitate future coding.
Notes for extension methods:
1. It must have at least one parameter;
2. The first parameter must be appended with the this keyword;
3. The first parameter cannot have any other modifier (out/ref)
4. The first parameter cannot be of the pointer type.
5. C # only supports extension methods, but does not support extension attributes and Extension events;
6. namespace System can be used for the namespace of the extension method, but it is not recommended;
7. The class defining the extension method is a static class;
Write it here today! Thank you for your support!
The original Article is on my personal website.
C language ^ how to use
A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010
B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011
^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.
//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].
Symbol in C Language <Yes
Left shift operator (<)
Removes all the binary bits of an operation object from the left and adds 0 to the right ).
For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,
Move 1 to the left and then a = a * 2;
If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)
Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.
The operand shifts one digit to the right, which is equivalent to dividing the number by 2.
For example, a = a> 2 shifts the binary bit of a two places to the right,
0 or 1 to see whether the number is positive or negative.