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

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;
    using System.Text.RegularExpressions;
    using System.Linq;
    
    namespace codeTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str = "12312312 123123 123123";
                Console.WriteLine(str.WordCount());
                MyClass mylClass = new MyClass() { str = "123123123" };
                Console.WriteLine(mylClass.WordCount());
                Console.ReadLine();
            }
    
    
        }
    
        /// <summary>
        /// 扩展方法必须在静态类里面,扩展方法也必须是静态的,首参数为this
        /// </summary>
        public static class stringExtension
        {
            public static int WordCount(this string str)
            {
                return str.Count();
            }
    
            public static int WordCount(this MyClass mylClass)
            {
                return mylClass.str.Count();
            }
        }
    
        public class MyClass
        {
            public string str;
        }
    
    
    
    }
  • 相关阅读:
    SpringBoot项目设置maven打包时间
    SpringBoot热部署配置
    Git笔记
    SpringBoot LogBack日志配置
    CURL使用教程
    Linux 安装Docker及使用
    转发和重定向的区别
    16周作业
    16
    15周
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/4790455.html
Copyright © 2011-2022 走看看