c# 扩展方法 入门小例

黄舟
黄舟 原创
2017-03-03 13:35:58 974浏览

C#扩展方法 入门小例

扩展方法的定义:

l 必须是静态类,静态方法

l 第一个参数带有关键字”this”,表示把这个方法赋给哪个类型

代码说明:这里的例子是写了一个静态类,myExtension,一个扩展方法Add,表示所有的INT类型的数字都将具有调用这个Add方法的能力,条件是引入MyExtension的命名空间。

下面让我们看一下用法:

代码说明:这段代码做的事是声明了一个int类型的数字并赋值为7,然后调用Add方法的时候你将看到智能感知如下:

可以看到扩展方法用了向下方向的箭头标记。然后调用就可以了,给它任意个int,由于我用了params关键字,将自动解析为int数组,之后用rlt变量进行接收,显示出来就会看到结果:



示例2:


写了一个扩展string的方法,可以把英文标准化,例如 hEllo WORld 传递进去 将会输出,Hello World


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace wpfLab1
{
    public static class StrExtensenClass
    {
        public static string GetNormalFormat(this string s)
        {
            s = RemoveExtraSpace(s);
            string[] words = s.Split(' ');
            string ret = "";
            foreach (var word in words)
            {
                ret += StrFstChrUpr(word) + " ";
            }
            return ret;
        }


        public static string RemoveExtraSpace(this string s)
        {
            if (s == null || s.Length <= 1)
            {
                return s;
            }


            bool lastChrIsSpace = false;
            string ret = "";
            foreach (var chr in s)
            {
                if (chr == ' ')
                {
                    if (lastChrIsSpace)
                    {
                        continue;
                    }
                    else
                    {
                        lastChrIsSpace = true;
                        ret += chr;
                    }
                }
                else
                {
                    ret += chr;
                    lastChrIsSpace = false;
                }


            }
            return ret;
        }


        private static string StrFstChrUpr(string s)
        {
            if (s == null || s.Length < 1)
            {
                return s;
            }
           
            string lowerStr = s.ToLower().Remove(0, 1);
            string upperStr = Char.ToUpper(s[0]).ToString();
            return (upperStr + lowerStr);
        }
    }
}

使用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using wpfLab1;
namespace wpfLab1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        private void btnHello_Click(object sender, RoutedEventArgs e)
        {
            string s = "hEllo   wOrLd,  hi,   world ,   aa dd  dw   WWdd a ";


            lblHello.Content = s.GetNormalFormat();
            
        }
    }
}

以上就是c# 扩展方法 入门小例的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。