18. C # extended methods (Chapter 10, 10.1-10.2 ),

Source: Internet
Author: User

18. C # extended methods (Chapter 10, 10.1-10.2 ),

Today's topic: Let's talk about the expansion method. I really lament my article. It's a miserable one. Let's review the article, looking at it, I also forgot what kind of mentality I wrote at that time. Haha, modern people are so casual and may be too indifferent. Then I wrote it, which will always be helpful, there will also be gains.

The extension method appeared in front of us starting from C #3. It has the advantages of static methods and makes our code more readable. We can call static methods like an instance method. Before the extension method appeared, we often encountered static tool classes in the Code (of course, even if we use static tool classes now, they are the wisdom of our predecessors ), such as a string help class and time Conversion Tool class. Imagine that when we get an encapsulated class library, the source code is invisible. We are only the users of the class library, but the developers of the class library are not perfect ideas and there are always some omissions. To put it simply, for example, if I get a piece of code, there is a Room class in the code, as shown below:

1 class Room2 {3     public string Name { get; set; }4 }

The above Code cannot be modified. Imagine how can we assign a value to the Name attribute of an object? We can use an instance Room object to assign values directly, but now I want to add a SetName Method to the Room class, the extension method will help us. For example:

1 static class RoomExt 2 {3 public static void SetName (this Room, string name) 4 {5 room. name = name; 6} 7} 8 9 Room room = new Room (); 10 room. name = "caotang"; 11 Console. writeLine (room. name); 12 room. setName (""); 13 Console. writeLine (room. name); 14 Console. readKey ();

In the above Code, the SetName method is an extension method of the Room class, which can be called like an instance method. The Room class developer forgets to add a SetName Method to the Room class, so we will use the extension method to add a method to the Room class. The call method is the same as the instance method. The first parameter of SetName is the extension type. Specify the class to be extended and use the keyword "this". this parameter is only required. Please follow this rule. The modifier of the method is static. If the code we write is an external call, use the public keyword and the pirvate keyword, which can only be used in the extension method for its extension.

1 room. SetName (""); // call the SetName method, which is internally implemented as RoomExt. setName (room, "")

The above code is the same as our static tool. Yes, the code can be more readable with the extension method. Of course, the above Code is just a simple use. In our normal use, the classes we write will certainly add appropriate instance methods, even for class libraries, this will also allow the class library developer to add a method. However, if Microsoft developers do not provide a proper method, you can't do it by yourself. So the extension method is often used to extend the system type. Extension methods are similar to static methods, but they must also have the following features:

  Find the appropriate method

When we use SetName, we will first find this method in the instance method. If not, we will find the method with the signature from the introduced namespace, there may also be heavy loads, so you can find and execute methods based on "who is better.

  Expand the null type

It is actually an extension of the object, such

1 static class objectExt2 {3     public static bool IsNull(this object o)4     {5         return o == null;6     }7 }

Use:

 1 static class objectExt 2 { 3     public static bool IsNull(this object o) 4     { 5         return o == null; 6     } 7 } 8  9 10 Room room1 = null;11 Console.WriteLine(room1.IsNull());

If you want to use IsNull, you can add an instance method IsNull in the class, or use a static tool to determine whether an instance is null. The instance method can be reloaded by using the extension method, as shown in figure

1 class Room2 {3     public string Name { get; set; }4 5     public bool IsNull()6     {7         return this == null;8     }9 }

When an instance method IsNull is added to the Room class, when IsNul is called, the extension method will never be called. Remember the sequence of "finding methods" mentioned above.

Please make an axe.

 

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.