C# this扩展方法
类别:编程学习 浏览量:1036
时间:2014-11-27 C# this扩展方法
C# this扩展方法扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用。
C#扩展方法第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。
C# this扩展方法实例
实例1、给string 类型增加一个Add方法,该方法的作用是给字符串增加一个字母a
//必须是静态类才可以添加扩展方法
Static class Program
{
static void Main(string[] args)
{
string str = "quzijing";
//注意调用扩展方法,必须用对象来调用
string Newstr = str.Add();
Console.WriteLine(Newstr);
Console.ReadKey();
}
//声明扩展方法
//扩展方法必须是静态的,Add有三个参数
//this 必须有,string表示我要扩展的类型,stringName表示对象名
//三个参数this和扩展的类型必不可少,对象名可以自己随意取如果需要传递参数,//再增加一个变量即可
public static string Add(this string stringName)
{
return stringName+"a";
}
}
实例2、给自定义的类型增加一个扩展方法,并增加一个传递的参数
(1)、声明一个Student类,它包含了两个方法StuInfo,getStuInfo
public class Student
{
public string StuInfo()
{
return "学生基本信息";
}
public string getStuInfo(string stuName, string stuNum)
{
return string.Format("学生信息:\\n" + "姓名:{0} \\n" + "学号:{1}", stuName, stuNum);
}
}
(2)、声明一个名为ExtensionStudentInfo的静态类,注意必须为静态
这个类的作用就是包含一些我们想要扩展的方法,在此我们声明两个Student类型的扩展方法,Student类型为我们自定义的类型。
public static class ExtensionStudentInfo
{
//声明扩展方法
//要扩展的方法必须是静态的方法,Add有三个参数
//this 必须有,string表示我要扩展的类型,stringName表示对象名
//三个参数this和扩展的类型必不可少,对象名可以自己随意取如果需要传递参数,再增加一个变量即可
public static string ExtensionStuInfo(this Student stuName)
{
return stuName.StuInfo();
}
//声明扩展方法
//要扩展的方法必须是静态的方法,Add有三个参数
//this 必须有,string表示我要扩展的类型,stringName表示对象名
//三个参数this和扩展的类型必不可少,对象名可以自己随意取如果需要传递参数,在此我们增加了两个string类型的参数
public static string ExtensionGetStuInfo(this Student student, string stuname, string stunum)
{
return student.getStuInfo(stuname, stunum)+"\\n读取完毕";
}
}
(3)、使用自定义类的扩展方法,注意必须要用对象来调用扩展方法
static void Main(string[] args)
{
Student newstudent = new Student();
//要使用对象调用我们的扩展方法
string stuinfo = newstudent.ExtensionStuInfo();
Console.WriteLine(stuinfo);
//要使用对象调用我们的扩展方法
string stuinformation = newstudent.ExtensionGetStuInfo("quzijing", "20081766");
Console.WriteLine(stuinformation);
Console.ReadKey();
}
实例3、为string扩展一个验证邮件类
(1)、扩展方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
//声明扩展方法的步骤:类必须是static,方法是static,
//第一个参数是被扩展的对象,前面标注this。
//使用扩展方法的时候必须保证扩展方法类已经在当前代码中using
namespace 扩展方法
{
//扩展方法必须是静态的
public static class StringHelper
{
//扩展方法必须是静态的,第一个参数必须加上this
public static bool IsEmail(this string _input)
{
return Regex.IsMatch(_input, @"^\\w+@\\w+\\.\\w+$");
}
//带多个参数的扩展方法
//在原始字符串前后加上指定的字符
public static string Quot(this string _input, string _quot)
{
return _quot + _input + _quot;
}
}
}
(2)、使用方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 扩展方法
{
class Program
{
static void Main(string[] args)
{
string _myEmail = "abc@163.com";
//这里就可以直接使用string类的扩展方法IsEmail了
Console.WriteLine(_myEmail.IsEmail());
//调用接收参数的扩展方法
Console.WriteLine(_myEmail.Quot("!"));
Console.ReadLine();
}
}
}
标签:this
您可能感兴趣
- javascript中的this判定(Javascript中函数分类&this指向的实例详解)
- C#中this的作用
- vue路由跳转自动定位在哪里(Vue路由this.route.push跳转页面不刷新的解决方案)
- C# this扩展方法
- vue中什么时候需要set属性(Vue.set和this.$set使用和区别)
- vue引用js库报错(Vue插件报错:Vue.js is detected on this page.问题解决)
- jQuery里$(this)和this的区别有哪些
- jquery中return this.each的作用
- js中this的用法
- js中的this与with关键字
- C#中的base和this关键字
- 白蓝色穿搭(白蓝色衣服配什么裤子)
- 天空是什么颜色(天空是什么颜色的英语)
- 高马尾扎发(高马尾扎发教程视频)
- 这里输入关键词(请手动输入关键词)
- 小说 顾瑾岚拿出一套飞行棋,别说你连飞行棋都不会哦(顾瑾岚拿出一套飞行棋)
- 金品公司 界界乐中秋限定飞行棋礼盒 露营藤篮礼盒全新上市(界界乐中秋限定飞行棋礼盒)
热门推荐
- laravel获取数据(laravel框架添加数据,显示数据,返回成功值的方法)
- curl怎么设置方法(关于Curl在Swoole协程中的解决方案详析)
- css3特效旋转菜单(CSS3实现的水平标题菜单)
- dedecms分页修改(Dedecms图片加上Alt和Title等属性的修改方法)
- sql查询优化最快的方法(必备 SQL 查询优化技巧提升网站访问速度)
- oracle RMAN CONFIGURE配置项
- laravel框架知识点(Laravel 不同生产环境服务器的判断实践)
- css安全模式(仅针对IE8有效的CSS Hack猎奇写法)
- mysql8.0.12安装教程图解(MySql8.023安装过程图文详解首次安装)
- 前端用图片隐藏请求接口(前端实现打印图像功能)