C# 扩张方法的语法

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "askljd sadsa dsakld sa?dsad";
            Console.WriteLine(str.GetWordCount());
            Console.ReadKey();

        }
    } 
    /// <summary>
    /// 扩展方法的写法:静态类-->静态方法 
    /// </summary>
    public static class stringExtension 
    {
        /// <summary>
        /// 语法 this 对那个类型的扩展 参数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int GetWordCount( this string str)
        {
            return str.Split(new char[] {' ','?',':',',','.'},StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }
}

 

posted @ 2019-09-07 22:44  liliyou  阅读(174)  评论(0编辑  收藏  举报