没有合适的资源?快使用搜索试试~ 我知道了~
首页c# this关键字用法代码详解
资源详情
资源评论
资源推荐

c# this关键字用法代码详解关键字用法代码详解
在本篇文章里小编给大家整理的是关于c# this关键字用法以及相关实例代码,有兴趣的朋友们可以学习下。
用法用法1 为原始类型扩展方法为原始类型扩展方法
先说一下,this 后面跟的类型,就是要拓展方法的类型。注意要写在静态类中的静态方法,不然有些情况下访问
/// <summary>
/// 扩展类 用于为原始类扩展方法
/// </summary>
public static class AM_Extends
{
/// <summary>
/// 为string类扩展了一个child方法,实现某功能
/// </summary>
/// <param name="str"></param>
/// <param name="new_str"></param>
public static void Child( this string str,string new_str)
{
object obj = str;
str=new_str;
}
}
定义扩展方法
private void Form1_Load(object sender, EventArgs e)
{
string st1 = "123";
string st2 = "";
string st3 = "";
st3 = st2.Child(st1);//st3的值为“123”
}
调用实例
用法用法2 this代表当前类的实例对象代表当前类的实例对象
用法用法3 用用this串联构造函数串联构造函数
public class Test
{
public Test()
{
Console.WriteLine("无参构造函数");
}
// this()对应无参构造方法Test()
// 先执行Test(),后执行Test(string text)
public Test(string text) : this()
{
Console.WriteLine(text);
Console.WriteLine("有参构造函数");
}
}
用法4 索引器(基于索引器封装EPList,用于优化大数据下频发的Linq查询引发的程序性能问题,通过索引从list集合中查询数
据)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace MyDemo.Web
{
/// <summary>
/// EPList 支持为List创建索引
/// </summary>
/// <typeparam name="T">类型</typeparam>
public class EPList<T>
{
#region 成员变量
/// <summary>
/// 索引
/// </summary>
















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0