C # this usage series (2) use the this modifier to extend the method for the original type,

Source: Internet
Author: User

C # this usage series (2) use the this modifier to extend the method for the original type,

Define a static class. The class defines static methods. Add the this modifier before the method parameter type to extend the class behind this modifier.

Example:

Namespace Demo {
// The class here must be a static class public static class Json {
// The method must be a static method.
// This modifier is followed by the string type, that is, the ToJson method is extended for the string type, and the conversion result is the object public static object ToJson (this string Json) {return Json = null? Null: JsonConvert. DeserializeObject (Json );}
// The type behind this modifier is object, that is, the ToJson method is extended for the object type, and the conversion result is the Json string public static string ToJson (this object obj) {var timeConverter = new IsoDateTimeConverter {DateTimeFormat = "yyyy-MM-dd HH: mm: ss"}; return JsonConvert. serializeObject (obj, timeConverter);} public static string ToJson (this object obj, string datetimeformats) {var timeConverter = new IsoDateTimeConverter {DateTimeFormat = datetimeformat S}; return JsonConvert. SerializeObject (obj, timeConverter);} public static T ToObject <T> (this string Json) {return Json = null? Default (T): JsonConvert. DeserializeObject <T> (Json);} public static List <T> ToList <T> (this string Json) {return Json = null? Null: JsonConvert. DeserializeObject <List <T> (Json);} public static DataTable ToTable (this string Json) {return Json = null? Null: JsonConvert. DeserializeObject <DataTable> (Json);} public static JObject ToJObject (this string Json) {return Json = null? JObject. parse ("{}"): JObject. parse (Json. replace ("& nbsp;", "") ;}} public class User {public string ID {get; set;} public string Code {get; set ;} public string Name {get; set ;}} class Program {static void Main (string [] args) {try {List <User> users = new List <User> {new User {ID = "1", Code = "zs", Name = "James "}, new User {ID = "2", Code = "ls", Name = "" }}; // List is extended with the ToJson method, used to convert string json = users. toJson (); // The ToJson method is extended for converting object obj = json. toJson (); // The ToList method is extended for string type conversion List users = json. toList <User> ();} catch (Exception ex) {Console. writeLine (ex);} finally {Console. readLine ();}}}}

Next, share the usage of this keyword in Indexer

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.