this的五种用法:

1.使用被掩盖的成员变量:

class AA

{

int a;

public void set1(int a)

{

this.a = a;//right

}

public void set2(int a)

{

a = a;//会有警告:“对同一变量进行赋值;是否希望对其他变量赋值?”;

}

}

2.把这个对象传给其他函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class BB
{
static public void set(AA t,int a)
{
t.set1(a);
}
}
class AA
{
int a;
public void set1(int a)
{
this.a = a;
}
public void set2(int a)
{
BB.set(this, a);
}
public int get()
{
return a;
}
} class Program
{
static void Main(string[] args)
{
AA t = new AA();
t.set2();
Console.WriteLine(t.get());
}
}
}

3.With Indexers(索引器)//这个暂时不懂

4.调用其他构造函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication2
{
class AA
{
int a, b;
public AA(int a)
{
this.a = a;
b = -;
print();
}
public AA(int a,int b):this(a)
{
this.b = b;
print();
}
void print()
{
Console.WriteLine("{0} {1}", a, b);
}
}
class Program
{
static void Main(string[] args)
{
AA t = new AA();
t = new AA(, );
}
}
}

输出:

1 -1

1 -1

1 1

5.是代码更明确

如把

        public AA(int a)
{
this.a = a;
b = -;
print();
}

改为

        public AA(int a)
{
this.a = a;
this.b = -;
print();
}

C#this的五种用法的更多相关文章

  1. webpack解惑:require的五种用法

    我之前在 <前端搭环境之从入门到放弃>这篇文章中吐槽过,webpack中可以写commonjs格式的require同步语法,可以写AMD格式的require回调语法,还有一个require ...

  2. webpack解惑:require的五种用法 (转)

    我之前在 <前端搭环境之从入门到放弃>这篇文章中吐槽过,webpack中可以写commonjs格式的require同步语法,可以写AMD格式的require回调语法,还有一个require ...

  3. webpack中,require的五种用法

    a.js: module.exports = function(x){ console.log(x); } 一,commonjs同步: var b = require('./a');b('你好')// ...

  4. Android Toast 总结(五种用法)

    Toast大家都很熟,不多说.直接上图上代码. 具体代码如下: main.xml: <?xml version="1.0" encoding="utf-8" ...

  5. Wix 安装部署教程(十五) --CustomAction的七种用法

    在WIX中,CustomAction用来在安装过程中执行自定义行为.比如注册.修改文件.触发其他可执行文件等.这一节主要是介绍一下CustomAction的7种用法. 在此之前要了解InstallEx ...

  6. css详解position五种属性用法及其含义

    position(定位) position - 作为css属性三巨头(position.display.float)之一,它的作用是用来决定元素在文档中的定位方式.其属性值有五种,分别是 - stat ...

  7. EntityFramework嵌套查询的五种方法

    这样的双where的语句应该怎么写呢: var test=MyList.Where(a => a.Flows.Where(b => b.CurrentUser == “”) 下面我就说说这 ...

  8. 转:Windows Socket五种I/O模型

    原文转自:  Windows Socket五种I/O模型 Winsock 的I/O操作: 1. 两种I/O模式 阻塞模式:执行I/O操作完成前会一直进行等待,不会将控制权交给程序.套接字 默认为阻塞模 ...

  9. 【转】Java 枚举7常见种用法

    原文网址:http://softbeta.iteye.com/blog/1185573 Java 枚举7常见种用法 博客分类: java java枚举enmu  原创地址:http://blog.li ...

随机推荐

  1. Js收藏-转

    /** * <P> Title: JavaScript Util </P> * <P> Description: JavaScript 工具 </P> ...

  2. C语言基础学习基本数据类型-其他整数类型

    其他整数类型 初学C语言时,int类型会满足你对整数的大多数需求. C语言还提供了三个关键字用以修饰基本的整数类型:short.long和unsigned.有以下几个注意点: (1)C语言没有具体规定 ...

  3. 这样就算会了PHP么?-5

    汇集一点关于数据深入一些的几个函数,去重,弹出,加入,查找.... <?php $str = "时装,休闲,职业装"; $strs = explode(",&quo ...

  4. 二极管IN4001~IN4007参数

    电压范围50~1000V 正向导通电流1A 导通电压降:1.1V 具体见下图:

  5. 运用Swagger 添加WebAPI 文档

    1. Go to Web link https://www.nuget.org/packages/Swashbuckle/ and check which version do we want. 2. ...

  6. 【转】Windows 7/8/8.1 硬盘安装法实现 ubuntu 14.04 双系统

    原文网址:http://www.cnblogs.com/chenguangqiao/p/4219532.html 一.软件准备 1. 下载 Ubuntu 系统镜像:http://www.ubuntu. ...

  7. NOI2013 快餐店

    http://uoj.ac/problem/126 总的来说,还是很容易想的,就是有点恶心. 首先,很明显只有一个环. 我们先找出这个环,给各棵树编号id[i],然后各棵树分别以环上的点为根,求出每个 ...

  8. C++链接器工具错误:LNK2001, LNK2019(转载)

    这是归属于链接器工具错误 这一类. 无法解析的外部符号“symbol” 代码引用了链接器无法在库和对象文件中找到的内容(如函数.变量或标签). 可能的原因 代码请求的内容不存在(例如,符号拼写错误或使 ...

  9. 瑞柏匡丞谈中国移动app的国际进阶路

    当今3.0互联时代,已然形成了一个移动化,互动化,全球化的完整体系.瑞柏匡丞也在常年与国内外客户的交流沟通中有了自己的些许见解. 国内的移动产业的发展已然非常迅速,但也正是因为各类企业的不断崛起,能够 ...

  10. C++ 中 struct和class 的区别

    来自:http://hi.baidu.com/pengxiangbobin19890125/blog/item/b05586eee77300212df53411.html   C++ prime  中 ...