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();
}
}
}
C# this扩展方法的更多相关文章
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
- .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类
.NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...
- 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇
最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...
- C#的扩展方法解析
在使用面向对象的语言进行项目开发的过程中,较多的会使用到“继承”的特性,但是并非所有的场景都适合使用“继承”特性,在设计模式的一些基本原则中也有较多的提到. 继承的有关特性的使用所带来的问题:对象的继 ...
- 扩展方法(C#)
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 下面的示例为String添加 ...
- 扩展方法解决LinqToSql Contains超过2100行报错问题
1.扩展方法 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...
- C#扩展方法
扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法就相当于一个马甲,给一个现有类套上,就可以为这个类添加其他方法了. 马甲必须定义为stati ...
- 枚举扩展方法获取枚举Description
枚举扩展方法 /// <summary> /// 扩展方法,获得枚举的Description /// </summary> /// <param name="v ...
- 扩展方法 1 简单的string扩展方法
这里是关于 String的简单扩展方法 (静态类 静态方法 this 类型 这里是string) static class Program { static void Main(string[] ar ...
- C#中的扩展方法
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 以上是msdn官网对扩展方 ...
随机推荐
- Visual Studio 2017的安装与使用
Visual Studio 2017的安装与使用 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1. 下载Visual Studio 2017 在官网 ...
- 虚拟机配置Linux上网环境
概要:在虚拟机安装CentOS6.5的环境后,配置NAT模式,修改系统文件支持上网. (1)ip地址的配置,IP地址的子网掩码为255.255.255.0. (2)网关的指定,也就是默认路由,当我们需 ...
- requests 进阶用法学习(文件上传、cookies设置、代理设置)
一.文件上传 1.模拟网站提交文件 提交此图片,图片名称:timg.jpg import requests files={ 'file':open('timg.jpg','rb') } respons ...
- 【English】20190320
valid有效的 [ˈvælɪd] solitary独立的 [ˈsɑ:ləteri] data definition not valid unless solitary qualifying有资格的 ...
- SQL解析在美团的应用
https://tech.meituan.com/SQL_parser_used_in_mtdp.html 数据库作为核心的基础组件,是需要重点保护的对象.任何一个线上的不慎操作,都有可能给数据库带来 ...
- 性能测试中的最佳用户数、最大用户数、TPS、响应时间、吞吐量和吞吞吐率
一:最佳用户数.最大用户数 转:http://www.cnblogs.com/jackei/archive/2006/11/20/565527.html 二: 事务.TPS 1:事务:就是用户某一步 ...
- jsLibrary.js
以前看犀牛书收藏和组合别人的库. ; (function () { 'use strict'; if (!Date.now) Date.now = function () { return new D ...
- 偶现bug如何处理?
请先允许我对此类bug进行吐槽,相信做测试的同学都碰见过这种bug! 我们在测试过程中经常会碰见一类很头疼的bug,就是偶现性的bug,所谓偶现性,是相对于必现而言,这类bug有些可以有重现路径,但是 ...
- Sqlserver查询死锁及杀死死锁的方法
-- 查询死锁 select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName from sy ...
- element ui 时间 date 差一天
let BirthdayYMD = common.formatDate(this.addForm.Dendline); this.addForm.Dendline = new Date(Birthda ...
{
static void Main(string[] args)
{
}
}