C#中this的作用
2020-12-13 13:41
标签:style http os ar 使用 strong sp div on 一、C# this指针的几种用法 1、限定被相似的名称隐藏的成员 2、将对象作为参数传递到别的方法中 3、声明索引器 二、C#中this的总结 1、this关键字引用被访问成员所在的当前实例。静态成员函数没有this指针。this关键字可以用来从构造函数,实例方法和实例化访问器中访问成员。 不能在静态方法。静态属性访问器或者域声明的变量初始化程序中使用this关键字,这将会产生错误。 2、在类的构造函数中出现的this作为一个值类型表示对正在构造的对象本身的引用。 3、在类的方法中出现this作为一个值类型表示对调用该方法的对象的引用。 4、在结构的构造函数中出现的this作为一个变量类型表示对正在构造的结构的引用。 5、在结构的方法中出现的this作为一个变量类型表示对调用该方法的结构。 C#中this的作用 标签:style http os ar 使用 strong sp div on 原文地址:http://www.cnblogs.com/gc2013/p/4053051.html

public class ThisName


{

public string name = "张三";

public int num = 55;

public ThisName() { }

public void GetThisName(string name, int num)

{

name = this.name;//调取全局变量name

num = this.num;//调取全局变量num

HttpContext.Current.Response.Write("参数name的值为:"+name+";参数num的值为:"+num);//输出结果为"参数name的值为张三;参数num值为:55"

}

}



public class ThisFF


{

public ThisFF()

{

}

public string shuju()

{

return "This指针当做方法传递";

}



public void f1(ThisFF ff)

{

HttpContext.Current.Response.Write(ff.shuju());

}



public void f()

{

f1(this);//this在这里可以理解为 ThisFF ff=news ThisFF();当前类的实例

}

}



ThisFF ff=news ThisFF();//方法实例

ff.f();//调用方法 输出结果为: This指针当做方法传递



private int _Sy;


public int this[int sy]


{

get { return _Sy;}

set { sy=_Sy;}

}

上一篇:数据结构与算法 - 线性表
下一篇:Java Optional类