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 ;}}}