首頁  >  文章  >  後端開發  >  c# 擴充方法 入門小例

c# 擴充方法 入門小例

黄舟
黄舟原創
2017-03-03 13:35:581209瀏覽

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 == &#39; &#39;)
                {
                    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)! ################
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn