zoukankan      html  css  js  c++  java
  • C# 扩展方法

    https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/how-to-implement-and-call-a-custom-extension-method

    namespace CustomExtensions
    {
        // Extension methods must be defined in a static class.
        public static class StringExtension
        {
            // This is the extension method.
            // The first parameter takes the "this" modifier
            // and specifies the type for which the method is defined.
            public static int WordCount(this String str)
            {
                return str.Split(new char[] {' ', '.','?'}, StringSplitOptions.RemoveEmptyEntries).Length;
            }
        }
    }
    namespace Extension_Methods_Simple
    {
        // Import the extension method namespace.
        using CustomExtensions;
        class Program
        {
            static void Main(string[] args)
            {
                string s = "The quick brown fox jumped over the lazy dog.";
                // Call the method as if it were an
                // instance method on the type. Note that the first
                // parameter is not specified by the calling code.
                int i = s.WordCount();
                System.Console.WriteLine("Word count of s is {0}", i);
            }
        }
    }
    

      

  • 相关阅读:
    关于MFC库和CRT库冲突的分析
    C++ Traits技术
    C/C++的参数传递机制
    C++与正态分布
    前端JavaScript
    python 前端 css
    python 前端 html
    MySQL 数据库
    网络编程 生产者消费者模型 GiL

  • 原文地址:https://www.cnblogs.com/merlinzjl/p/14730085.html
Copyright © 2011-2022 走看看