本文导读:扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的。 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。 扩展方法当然不能破坏面向对象封装的概念,所以只能是访问所扩展类的public成员。

扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用。

C#扩展方法第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。

 

C# this扩展方法实例

 

实例1、给string 类型增加一个Add方法,该方法的作用是给字符串增加一个字母a

 

 
C# 代码   复制
C# this扩展方法

C# this扩展方法//必须是静态类才可以添加扩展方法
C# this扩展方法Static class Program
{
C# this扩展方法static void Main(string[] args)
{
C# this扩展方法string str = "quzijing";
C# this扩展方法//注意调用扩展方法,必须用对象来调用 
C# this扩展方法            string Newstr = str.Add();
C# this扩展方法            Console.WriteLine(Newstr);
C# this扩展方法            Console.ReadKey();
C# this扩展方法        }
C# this扩展方法//声明扩展方法
C# this扩展方法//扩展方法必须是静态的,Add有三个参数
C# this扩展方法//this 必须有,string表示我要扩展的类型,stringName表示对象名
C# this扩展方法//三个参数this和扩展的类型必不可少,对象名可以自己随意取如果需要传递参数,//再增加一个变量即可
C# this扩展方法
C# this扩展方法public static  string  Add(this string stringName)
{
C# this扩展方法return stringName+"a";
C# this扩展方法        }
C# this扩展方法}
C# this扩展方法

 

实例2、给自定义的类型增加一个扩展方法,并增加一个传递的参数

(1)、声明一个Student类,它包含了两个方法StuInfo,getStuInfo

 
 
C# 代码   复制
C# this扩展方法

C# this扩展方法public class Student
{
C# this扩展方法public string StuInfo()
{
C# this扩展方法return "学生基本信息";
C# this扩展方法        }
C# this扩展方法public  string getStuInfo(string stuName, string stuNum)
{
C# this扩展方法return string.Format("学生信息:\\n" + "姓名:{0} \\n" + "学号:{1}", stuName, stuNum);
C# this扩展方法        }
C# this扩展方法     }
C# this扩展方法

 

(2)、声明一个名为ExtensionStudentInfo的静态类,注意必须为静态

这个类的作用就是包含一些我们想要扩展的方法,在此我们声明两个Student类型的扩展方法,Student类型为我们自定义的类型。

 
 
C# 代码   复制
C# this扩展方法

C# this扩展方法public static class ExtensionStudentInfo
{
C# this扩展方法//声明扩展方法
C# this扩展方法//要扩展的方法必须是静态的方法,Add有三个参数
C# this扩展方法//this 必须有,string表示我要扩展的类型,stringName表示对象名
C# this扩展方法//三个参数this和扩展的类型必不可少,对象名可以自己随意取如果需要传递参数,再增加一个变量即可
C# this扩展方法        public static string ExtensionStuInfo(this Student stuName)
{
C# this扩展方法return stuName.StuInfo();
C# this扩展方法        }
C# this扩展方法//声明扩展方法
C# this扩展方法//要扩展的方法必须是静态的方法,Add有三个参数
C# this扩展方法//this 必须有,string表示我要扩展的类型,stringName表示对象名
C# this扩展方法//三个参数this和扩展的类型必不可少,对象名可以自己随意取如果需要传递参数,在此我们增加了两个string类型的参数
C# this扩展方法        public static string ExtensionGetStuInfo(this Student student, string stuname, string stunum)
{
C# this扩展方法return student.getStuInfo(stuname, stunum)+"\\n读取完毕";
C# this扩展方法        }
C# this扩展方法    }
C# this扩展方法

 

(3)、使用自定义类的扩展方法,注意必须要用对象来调用扩展方法

 
 
C# 代码   复制
C# this扩展方法

C# this扩展方法static void Main(string[] args)
{
C# this扩展方法            Student newstudent = new Student();
C# this扩展方法//要使用对象调用我们的扩展方法
C# this扩展方法            string stuinfo = newstudent.ExtensionStuInfo();
C# this扩展方法            Console.WriteLine(stuinfo);
C# this扩展方法//要使用对象调用我们的扩展方法
C# this扩展方法string stuinformation = newstudent.ExtensionGetStuInfo("quzijing", "20081766");
C# this扩展方法            Console.WriteLine(stuinformation);
C# this扩展方法            Console.ReadKey();
C# this扩展方法        }
C# this扩展方法

 

实例3、为string扩展一个验证邮件类

(1)、扩展方法

 
 
C# 代码   复制
C# this扩展方法

C# this扩展方法using System;
C# this扩展方法C# this扩展方法using System.Collections.Generic;
C# this扩展方法C# this扩展方法using System.Linq;
C# this扩展方法C# this扩展方法using System.Text;
C# this扩展方法C# this扩展方法using System.Text.RegularExpressions;
C# this扩展方法C# this扩展方法C# this扩展方法C# this扩展方法//声明扩展方法的步骤:类必须是static,方法是static,
C# this扩展方法C# this扩展方法//第一个参数是被扩展的对象,前面标注this。
C# this扩展方法C# this扩展方法//使用扩展方法的时候必须保证扩展方法类已经在当前代码中using
C# this扩展方法
C# this扩展方法namespace 扩展方法
C# this扩展方法{
C# this扩展方法C# this扩展方法//扩展方法必须是静态的
C# this扩展方法
C# this扩展方法public static class StringHelper
C# this扩展方法{
C# this扩展方法C# this扩展方法//扩展方法必须是静态的,第一个参数必须加上this
C# this扩展方法
C# this扩展方法public static bool IsEmail(this string _input)
C# this扩展方法{
C# this扩展方法C# this扩展方法return Regex.IsMatch(_input, @"^\\w+@\\w+\\.\\w+$");
C# this扩展方法C# this扩展方法        }
C# this扩展方法C# this扩展方法//带多个参数的扩展方法
C# this扩展方法C# this扩展方法//在原始字符串前后加上指定的字符
C# this扩展方法
C# this扩展方法public static string Quot(this string _input, string _quot)
C# this扩展方法{
C# this扩展方法C# this扩展方法return _quot + _input + _quot;
C# this扩展方法C# this扩展方法        }
C# this扩展方法C# this扩展方法    }
C# this扩展方法C# this扩展方法}
C# this扩展方法

 

(2)、使用方法

 
C# 代码   复制
C# this扩展方法

C# this扩展方法using System;
C# this扩展方法C# this扩展方法using System.Collections.Generic;
C# this扩展方法C# this扩展方法using System.Linq;
C# this扩展方法C# this扩展方法using System.Text;
C# this扩展方法C# this扩展方法namespace 扩展方法
C# this扩展方法{
C# this扩展方法C# this扩展方法class Program
C# this扩展方法{
C# this扩展方法C# this扩展方法static void Main(string[] args)
C# this扩展方法{
C# this扩展方法C# this扩展方法string _myEmail = "abc@163.com";
C# this扩展方法C# this扩展方法//这里就可以直接使用string类的扩展方法IsEmail了
C# this扩展方法
C# this扩展方法            Console.WriteLine(_myEmail.IsEmail());
C# this扩展方法C# this扩展方法//调用接收参数的扩展方法
C# this扩展方法
C# this扩展方法            Console.WriteLine(_myEmail.Quot("!"));
C# this扩展方法C# this扩展方法C# this扩展方法C# this扩展方法            Console.ReadLine();
C# this扩展方法C# this扩展方法        }
C# this扩展方法C# this扩展方法    }
C# this扩展方法C# this扩展方法}
C# this扩展方法

相关文章:

  • 2021-07-18
  • 2021-08-27
  • 2021-11-25
  • 2021-05-21
猜你喜欢
  • 2022-12-23
  • 2021-05-21
  • 2022-01-02
  • 2022-12-23
  • 2021-12-04
  • 2021-07-08
  • 2021-11-29
相关资源
相似解决方案