代码改变世界

简单剖析this在C#中的运用

2011-10-01 00:43  随风浪迹天涯  阅读(271)  评论(0编辑  收藏  举报

昨天看一本C#的书,写了有关this的用法。今天想起来,就写一个笔记,当做记忆吧。

其实,this说到底有3种用法(我所知道的),下面我很粗略的分析一下,如有不正之处,敬请指教:

1.限定被相似的名称隐藏的成员

这种情况一般式在使用变量的时候,使用2个相同的变量的时候,这个时候this就很有效果了。比如

 

public class FirstThisDemo
    {
        private string name;
        public FirstThisDemo(string name)
        {
            this.name = name;
        }

        public void PrintInformation()
        {
            Console.WriteLine("输入的名字为{0}", this.name);
        }
    }

  然后再控制台进行对它的调用:

  static void Main(string[] args)
        {
            FirstThisDemo firstdemo = new FirstThisDemo("Lanny");
            firstdemo.PrintInformation();
        }

  如你所见,输出的结果是:输入的名字为Lanny

 

但是如果你对name的前面不加this的话,比如:

public FirstThisDemo(string name)
        {
            name = name;
        }

  那么对不起,当你在控制台调用的时候就显示不出来。而且还会显示出一个警告来:

               警告 37 对同一变量进行赋值;是否希望对其他变量赋值? 

     这个就是this在限定成员中作用。

 

2.this在索引器的使用

this的第二个用处是可以在类中进行申明索引器,如果这样的话吗,就可以在类中很好对类中的成员进行遍历。

索引器使得对象可按照与数组相似的方法进行索引。
 特点如下:

☆get 访问器返回值。set 访问器分配值。

☆this 关键字用于定义索引器。

☆value 关键字用于定义由 set 索引器分配的值。

☆索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。

☆索引器可被重载。

☆索引器可以有多个形参,例如当访问二维数组时

直接看代码,先看一个简单的

 class MySecondDemo
    {
        private int[] test = new int[10];
        public int this[int index]
        {
            set
            {
                if (index > 0)
                {
                    test[index] = value;
                }
            }
            get
            {
                if (index > 0 && index < 10)
                {
                    return test[index];
                }
                else
                {
                    return 0;
                }
            }
        }
    }

  这里的意思是说对数组中的值存入,然后再进行可以想平时array一样,在指定的第几个进行读取。

 

MySecondDemo thisDemo = new MySecondDemo();
            for (int i = 0; i < 10; i++)
            {
                thisDemo[i] = i + 1;
            }
            Console.WriteLine(thisDemo[8]);

  上面输出指定的第8个元素,也就是9.

this的索引器,也可以对class进行索引。比如如下:

class Person
    {
        public Person()
        { }
        ArrayList lists = new ArrayList();
        private string name;
        private int age;
        private string province;
        public string Name
        {
            set { name = value; }
            get { return name; }
        }
        public int Age
        {
            set { this.age = value; }
            get { return this.age; }
        }
        public string Province
        {
            set { this.province = value; }
            get { return this.province; }
        }

        public Person(string name, int age, string province)
        {
            this.name = name;
            this.age = age;
            this.province = province;
        }
        public string this[string name, int age]
        {
            set
            {
                lists.Add(new Person(name, age, value));
            }
            get
            {
                foreach (Person person in lists)
                {
                    if (person.Age == age && person.name == name)
                    {
                        return person.province;
                    }
                }
                return "没有这个省份";
            }
        }

        /// <summary>
        /// 重载一个索引器
        /// </summary>
        /// <param name="province">传入省份</param>
        /// <returns></returns>
        public ArrayList this[string province]
        {

            get
            {
                ArrayList temp = new ArrayList();
                // if(lists)
                foreach (Person person in lists)
                {
                    if (person.province == province)
                    {
                        temp.Add(person);
                    }
                }
                return temp;
            }
        }
    }

  上面2个例子就是简单的this索引器的实例。

2.1 索引器和属性的区别

下面摘自MSDN的一段比较,MSDN 属性和索引器的区别连接

 3.this在用于构造函数声明

因为我们在平时对构造函数重载的时候,如果重载很多的话,会非常的麻烦,所以如果用调用this的话,就可以很好的进行处理。

使用方法:

可以使用如下的形式来声明实例构造函数:

『访问修饰符』【类名】(『形式参数表』) : this(『实际参数表』)

{

   【语句块】

}

其中的this表示该类本身所声明的、�%B