用法一 代表当前类的实例对象
class Program
{
static void Main(string[] args)
{
//Person实例化为对象p
Person p = new Person();
p.Age = 18;
Console.WriteLine(p.MyAge());
}
}
public class Person
{
//这个Age代表类的全局变量
public int Age { get; set; }
public string MyAge()
{
//这个Age代表方法的局部变量,将全局变量的Age+1赋值给局部变量
int Age=this.Age+1;
return $"今年{this.Age}岁,明年{Age}岁";
}
}

用法二 this()代表当前类的无参构造函数
namespace Demo
{
class Program
{
static void Main(string[] args)
{
Person p = new Person("李四");
p.Age = 18;
Console.WriteLine(p.MyAge());
}
}
public class Person
{
//无参构造函数
public Person()
{
this.Name = "张三";
Console.WriteLine("进入Person类的无参构造函数");
}
// this()代表无参构造函数Person()
// 先执行Person(),后执行Person(string Name)
public Person(string Name) : this()
{
this.Name = Name;
Console.WriteLine("进入Person类的有参构造函数");
}
public int Age { get; set; }
public string Name { get; set; }
public string MyAge()
{
int Age=this.Age+1;
return $"{this.Name}今年{this.Age}岁,明年{Age}岁";
}
}
}

用法三 为原始类型定义扩展方法,将对象作为参数传递给方法
场景:只显示一字符串的前10个字符,多余的用“***”省略号替代
(1)不用扩展方法,写1个静态方法
namespace Demo
{
class Program
{
static void Main(string[] args)
{
string str = "iloveyou8023";
string result = StringUtil.CutString(str, 10);
Console.WriteLine(result);
}
}
public class StringUtil
{
/// <summary>
/// 截取字符串
/// </summary>
/// <param name="str">要截取的字符串</param>
/// <param name="length">截取长度</param>
/// <returns>返回截取后的字符串</returns>
public static string CutString(string str, int length)
{
if (!string.IsNullOrEmpty(str))
{
if (str.Length <= length)
{
return str;
}
else
{
return str.Substring(0, length) + "***";
}
}
else
{
return "";
}
}
}
}
(2)使用扩展方法,定义一个string的扩展方法CutString
使用扩展方法要注意两个点:1)扩展方法必须定义在最上层的静态类中。2)使用this关键字将对象作为参数传递给方法。
namespace Demo
{
class Program
{
static void Main(string[] args)
{
string str = "iloveyou8023";
string result = str.CutString(10);
Console.WriteLine(result);
}
}
public static class StringExtension
{
/// <summary>
/// 截取字符串,多余部分用""***"代替
/// </summary>
/// <param name="str">源字符串</param>
/// <param name="length">截取长度</param>
/// <returns>返回截取后的字符串</returns>
public static string CutString(this string str, int length)
{
if (!string.IsNullOrEmpty(str))
{
if (str.Length <= length)
{
return str;
}
else
{
return str.Substring(0, length) + "***";
}
}
else
{
return "";
}
}
}
}
这样代码更简洁,就像用sting去调用Substring()等系统定义的方法一样。
用法四 申明索引器
using System;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
//索引器的使用
IndexerClass Indexer = new IndexerClass();
//“=”号右边对索引器赋值,其实就是调用其set方法
Indexer[0] = "张三";
Indexer[1] = "李四";
//输出索引器的值,其实就是调用其get方法
Console.WriteLine(Indexer[0]);
Console.WriteLine(Indexer[1]);
Console.WriteLine(Indexer[2]);
}
}
public class IndexerClass
{
private string[] name = new string[2];
//索引器必须以this关键字定义,其实这个this就是类实例化之后的对象
public string this[int index]
{
//实现索引器的get方法
get
{
if (index >= 0 && index < 2)
{
return name[index];
}
return "无效的值";
}
//实现索引器的set方法
set
{
if (index >= 0 && index < 2)
{
name[index] = value;
}
}
}
}
}
运行如下:

this不可用于静态方法中
1、尽量少用或者不用base和this。除了避开子类的名称冲突和在一个构造函数中调用其他的构造函数之外,base和this的使用容易引起不必要的结果。
2、在静态成员中使用base和this都是不允许的。原因是,base和this访问的都是类的实例,也就是对象,而静态成员只能由类来访问,不能由对象来访问。this关键字只能在实例构造函数、实例方法或实例访问器中使用。
3、base是为了实现多态而设计的。
4、使用this或base关键字只能指定一个构造函数,也就是说不可同时将this和base作用在一个构造函数上。
5、简单的来说,base用于在派生类中访问重写的基类成员;而this用于访问本类的成员,当然也包括继承而来公有和保护成员。
6、除了base,访问基类成员的另外一种方式是:显示的类型转换来实现。只是该方法不能为静态方法。
2万+




被折叠的 条评论
为什么被折叠?



