C#学习系列-this的使用

简介: 原文:C#学习系列-this的使用如有错误,欢迎指正。 1.代表当前类,在当前类中可使用this访问当前类成员变量和方法(需要注意的是 静态方法中不能使用this),也可用于参数传递,传递当前对象的引用。
原文: C#学习系列-this的使用

如有错误,欢迎指正。

1.代表当前类,在当前类中可使用this访问当前类成员变量和方法(需要注意的是 静态方法中不能使用this),也可用于参数传递,传递当前对象的引用。

下面贴代码:

    class Program
    {
        static void Main(string[] args)
        {
            thisClass testObj = new thisClass();
            Console.ReadLine();
        }
    }

    class thisClass
    {
        private string A { get; set; }
        public thisClass()
        {
            /*当前类this 访问类中属性A 静态方法无法访问A属性*/
            this.A = "Test String";
            Console.WriteLine(this.TestFun("TestFun :"));
        }
        private string TestFun(string args)
        {
            return args + this.A;
        }
    }

2.声明索引器

索引器:允许类和结构的实例按照与数组相同的方式进行索引,索引器类似与属性,不同之处在于他们的访问器采用参数,被称为有参属性,索引可以被重载,属于实例成员,不能声明为static。

下面贴代码:

    class Program
    {
        static void Main(string[] args) { indexClass intIndexClass = new indexClass(); intIndexClass[0] = new thisClass("intIndexClass 111"); intIndexClass[1] = new thisClass("intIndexClass 222"); indexClass stringIndexClass = new indexClass(); stringIndexClass["string1"] = new thisClass("stringIndexClass string1"); stringIndexClass["string2"] = new thisClass("stringIndexClass string2"); Console.ReadLine(); } } class indexClass { /*声明属性*/ private thisClass[] thisClassArr = new thisClass[10]; private Hashtable thisClassStrArr = new Hashtable(); /*创建索引器1 索引可以被重载,属于实例成员,不能声明为static*/ public thisClass this[int index] { get { return thisClassArr[index]; } set { this.thisClassArr[index] = value; } } /*创建索引器2*/ public thisClass this[string index] { get { return thisClassStrArr[index] as thisClass; } set { this.thisClassStrArr[index] = value; } } } class thisClass { private string A { get; set; } public thisClass(string str) { /*当前类this 访问类中属性A 静态方法无法访问A属性*/ this.A = str; Console.WriteLine(this.TestFun("TestFun :")); } private string TestFun(string args) { return args + this.A; } }

3.用于扩展方法

扩展方法的要素: 
1.此方法必须是一个静态方法 
2.此方法必须放在静态类中 
3.此方法的第一个参数必须以this开头,并且指定此方法是扩展自哪个类型

public static string DateToString(this DateTime dt)
{
      return dt.ToString("yyyy-mm-dd hh:mm:ss");
} 

static void Main(string[] args)
{
      DateTime now = DateTime.Now; 
      string time = now.DateToString();
      Console.WriteLine(time);
      Console.ReadKey();
} 

 

 

 我看了好像就这么多,其他还有补充的没?

目录
相关文章
|
1月前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
1月前
|
C#
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
|
1月前
|
C#
C#学习相关系列之yield和return的区别
C#学习相关系列之yield和return的区别
|
1月前
|
C#
C#学习相关系列之数组---常用方法使用(二)
C#学习相关系列之数组---常用方法使用(二)
|
1月前
|
存储 C#
C#学习系列相关之数组(一)---数组的定义与使用
C#学习系列相关之数组(一)---数组的定义与使用
|
1月前
|
C#
C#学习相关系列之常用符号介绍
C#学习相关系列之常用符号介绍
|
1月前
|
安全 编译器 C#
C#学习相关系列之多线程---lock线程锁的用法
C#学习相关系列之多线程---lock线程锁的用法
|
1月前
|
C#
C#学习相关系列之多线程---ConfigureAwait的用法
C#学习相关系列之多线程---ConfigureAwait的用法
|
1月前
|
C#
C#学习相关系列之多线程---TaskCompletionSource用法(八)
C#学习相关系列之多线程---TaskCompletionSource用法(八)
|
1月前
|
开发框架 .NET C#
C#学习相关系列之Linq用法---where和select用法(二)
C#学习相关系列之Linq用法---where和select用法(二)