Refactoring to extension method (3): support for chain writing, which makes us crazy

Source: Internet
Author: User

What is chain writing?

It's just like writing in the form of LINQ. For example, collection. Where (O => O. ID = 0). firstordefault ();

 

How to Implement chained writing?

The key is to return!

 

1. Chain writing, String. isnullorempty (), which is very fluent. "". isnullorempty () instead of string. isnullorempty ("")

 
Public static bool isnullorempty (this string Str) {return string. isnullorempty (STR );}

2. chained writing method to determine whether it is empty

 

 
Public static bool isnull (this object OBJ) {return OBJ = NULL ;}

 

 

 

3. chained writing, replacing the as operator

 

 
Public static T as <t> (this object OBJ) {If (obj. isnull () throw new argumentnullexception ("OBJ"); Return obj is t? (T) OBJ: default (t );}

 

 

4. chained writing, the object does not stop doing things.

 

 
Public static t action <t> (this t target, Action <t> action) {Action (target); Return target ;}

 

 

5. Get the default value of the type, which is equivalent to default (T). What can be done with the type object? Use this extension method.

 

Public static class typeextension {public static object getdefaultvalue (this type) {var typecode = type. gettypecode (type); Return getdefualtvalue (typecode);} public static object getdefualtvalue (typecode) {Switch (typecode) {Case typecode. boolean: Return default (Boolean); Case typecode. byte: Return default (byte); Case typecode. CHAR: Return default (char); Case typecode. datetime: Return default (datetime); Case typecode. dbnull: Return default (dbnull); Case typecode. decimal: Return default (decimal); Case typecode. double: Return default (double); Case typecode. empty: return NULL; Case typecode. int16: Return default (int16); Case typecode. int32: Return default (int32); Case typecode. int64: Return default (int64); Case typecode. object: Return default (object); Case typecode. sbyte: Return default (sbyte); Case typecode. single: Return default (single); Case typecode. string: Return default (string); Case typecode. uint16: Return default (uint16); Case typecode. uint32: Return default (uint32); Case typecode. uint64: Return default (uint64); default: return NULL ;}}}

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.