.net 3.5新特性之用this关键字为类添加扩展方法

本文介绍如何使用 C# 的 this 关键字为现有类和接口添加扩展方法,包括自定义类 Student 和内置 String 类的扩展实例。通过扩展方法,可以不修改原始类的情况下增加新功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. public static class ClassHelper  
  2. {  
  3.     //用this 声明将要吧这个方法附加到Student对象  
  4.     public static bool CheckName(this Student stu)  
  5.     {  
  6.         if (stu.Name == "小明")  
  7.         {  
  8.             return true;  
  9.         }  
  10.         else  
  11.             return false;  
  12.     }  
  13.     //为String对象添加一个ChangeString 方法  
  14.     public static string ChangeString(this String s,string str)  
  15.     {  
  16.         return str + ":" + s;  
  17.     }  
  18. }  
  19.   
  20. public class Student  
  21. {  
  22.     public string Name { getset; }  
  23.   
  24.     public void Show()  
  25.     {  
  26.         this.CheckName();//Student拥有了CheckName这个方法  
  27.           
  28.     }  
  29.     public void ShowString()  
  30.     {  
  31.         string s = "aaa";  
  32.         s.ChangeString("哈哈");//String拥有了ChangeString这个方法  
  33.     }  
  34. }  

这个例子很简单,为自定义的Student类扩展了一个CheckName方法,该方法没有参数,然后又为String系统自带的类 扩展了一个ChangeString 方法,该方法有一个string  类型的参数,注意:扩展方法必须声明为static静态方法,并且放在静态类中。

 

this不仅可以扩展类方法,还可以扩展接口,使得任何继承自该接口的类都会拥有此扩展方法。这里我们修改上面这个例子:

[c-sharp]  view plain copy
  1. public static class ClassHelper  
  2.     {  
  3.         //用this 声明将要吧这个方法附加到IStudent对象  
  4.         public static bool CheckName(this IStudent stu)  
  5.         {  
  6.             if (stu.Name == "小明")  
  7.             {  
  8.                 return true;  
  9.             }  
  10.             else  
  11.                 return false;  
  12.         }  
  13.         //为String对象添加一个ChangeString 方法  
  14.         public static string ChangeString(this String s, string str)  
  15.         {  
  16.             return str + ":" + s;  
  17.         }  
  18.   
  19.   
  20.     }  
  21.   
  22.     public interface IStudent  
  23.     {  
  24.         public void Show();  
  25.         public string Name { getset; }  
  26.     }  
  27.   
  28.     public class StudentA : IStudent  
  29.     {  
  30.           
  31.   
  32.         public void Show()  
  33.         {  
  34.             this.CheckName();//Student拥有了CheckName这个方法  
  35.   
  36.         }  
  37.         public void ShowString()  
  38.         {  
  39.   
  40.             string s = "aaa";  
  41.             s.ChangeString("哈哈");//String拥有了ChangeString这个方法  
  42.               
  43.         }  
  44.     }  
  45.   
  46.     public class StudentB : IStudent  
  47.     {  
  48.   
  49.   
  50.         public void Show()  
  51.         {  
  52.             this.CheckName();//Student拥有了CheckName这个方法  
  53.   
  54.         }  
  55.         public void ShowString()  
  56.         {  
  57.             string s = "aaa";  
  58.             s.ChangeString("哈哈");//String拥有了ChangeString这个方法  
  59.   
  60.         }  
  61.     }  
,注意这个例子的CheckName由this Student 改成了 this IStudent,IStudent 是一个接口,StudentA和StudentB都继承了这个接口,他们都会拥有CheckName这个扩展方法。

 

显然这样做是有好处的,当第三方提供给你一个DLL,其中包括许多类,由于这些类都是封装在DLL中的,当你想扩展其中某个类的时候,你却不能修改类的源代码。这时候你就可以用this来扩展。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值