扩展方法

//扩展方法可以合并到扩展类型的实例上,因此扩展方法定义为静态类并且第一个参数为扩展当前实例加上this
  public static class StringExt
    {
        public static string SplitBySpace(this string str)
        {

            string strRes;
            //将字符串转换为数组
            char[] cs = str.ToCharArray();
            //join方法
            strRes = string.Join(" ",cs);
            return strRes;
        }
    }
   static void Main(string[] args)
        {
            string s = "abcdefg";
            //调用扩展方法
            Console.WriteLine(s.SplitBySpace());
            Console.ReadKey();

        }

  

猜你喜欢

转载自www.cnblogs.com/jimtang/p/8978454.html