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. 使用 Nginx 来反向代理多个 NoderCMS

    Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...

  2. uva 10260 - Soundex

    题目:编码翻译,有些字母有对应的数字,有的没有,如果连续对应的数字相同只输出一个. #include <iostream> #include <cstdlib> #includ ...

  3. Lua 学习笔记(二)

    七.再论lua函数 1.lua中的函数被认为是带有词法定界和第一类值    a.词法定界:被嵌套的函数可以访问外部函数的变量    b.第一类值: lua中的函数可以放在变量中    (函数指针?) ...

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

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

  5. C程序设计语言练习题1-1

    练习1-1 在你自己的系统中运行"hello, world"程序.再有意去掉程序中的部分内容,看看会得到什么出错信息. 代码如下: #include <stdio.h> ...

  6. ucos_ii 上锁函数OSSchedLock()函数透析

    因为任务调度时一般都是通过OSTIMEDLY()来实现.在这个函数中会对当前的任务执行挂起.同时查看任务调度表中是否有优先级合适的就绪任务.如果当前任务运行时调用OSSchedLock()给调度器上锁 ...

  7. 深入浅出scanf、getcha、gets、cin函数

    转:问题描述一:(分析scanf()和getchar()读取字符) scanf(), getchar()等都是标准输入函数,一般人都会觉得这几个函数非常简单,没什么特殊的.但是有时候却就是因为使用这些 ...

  8. Jasper_pass data_from main report to subReport (local CSV)

    <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>< ...

  9. HTML5面试题-备

    万不可投机取巧.只求当时过关,非长久之计也!(感谢大神分享) 面试有几点需要注意: 面试题目: 根据你的等级和职位变化,入门级到专家级:范围↑.深度↑.方向↑. 题目类型: 技术视野.项目细节.理论知 ...

  10. github三大步骤

    1)git init : 初始化当前目录,把这个目录变成Git可以管理的目录 2)git add [文件名称]:  把文件添加到仓库 3)git commit -m "对当前提交文件的描述& ...