zoukankan      html  css  js  c++  java
  • Implement and Call a Custom Extension Method

    Implement and Call a Custom Extension Method

    Author Kaden Kang 康翌

    2009-5-14

    How to Implement a Custom Extension Method

    1.        Define a static class to contain the extension method.

    2.        Create a static Method.

    3.        Using this keyword to extension method.

    Method Extension means developer can add some new method to existent CLR type open rules, not to generate sub class or recompile previous type.

    The following code shows how to implement a custom extension method.

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

     

    namespace MyExtensionMethod

    {

        /// <summary>

        /// Note: the class must have the static keyword

        /// </summary>

        public static class MyExtensionMethod

        {

            /// <summary>

            /// Extension method define

            /// </summary>

            /// <param name="str">string instance</param>

            /// <returns>string</returns>

            public static string MyString(this string str)

            {

                return str + " Kaden Kang";

            }

        }

    }

    Note: “This” means “string” type extension.

    How to Call a Custom extension Method

    The following code shows how to call a custom extension method.

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using MyExtensionMethod;

     

    namespace ExtensionMethodTest

    {

        class Program

        {

            static void Main(string[] args)

            {

                string str = "test";

                Console.WriteLine(str.MyString());

            }

        }

    }

    Reference

    http://msdn.microsoft.com/en-us/library/bb311042.aspx

  • 相关阅读:
    vue 引用本地图片
    antdVue
    Nest.js —— A progressive Node.js framework
    20184313《网络对抗技术》Exp8 Web综合
    20184313《网络对抗技术》Exp7 网络欺诈防范
    20184313《网络对抗技术》Exp6 MSF应用基础
    20184313《网络对抗技术》Exp5 信息搜集与漏洞扫描
    网页激知序列号之途径(网友提供技术参考)
    【转】Delphi中正则表达式支持中文的写法
    遇到的一个奇怪问题
  • 原文地址:https://www.cnblogs.com/kangyi/p/1456827.html
Copyright © 2011-2022 走看看