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中文网(www.php.cn)!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号