• c# this关键字的理解


    this关键字引用类的当前实例

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

    2/将对象作为参数传递到其他方法

    3/声明索引器

    实际案例参考:

    //成员类
        public class Employee
        {
            private string _MyName;
            private int _MyAge;
            public string[] _arry = new string[5];
    
            public string Name { get { return this._MyName; } }
    
            public int Age { get {return this._MyAge; } }
    
            public Employee(string name, int age)
            {
                this._MyName = name;
                this._MyAge = age;
            }
            public string this[int param] {
                get { return _arry[param]; }
                set { _arry[param] = value; }
            }
    
            public void PrintInfo() {
                Print.DoPrint(this);
            }
    
            //改善C#程序的50种方法
        }
        //打印类
        public class Print {
            public static void DoPrint(Employee e)
            {
                Console.WriteLine("Name:{0}
    Age:{1}",e.Name,e.Age);
            }
    
        }

    执行代码 和结果

     Employee emp = new ConsoleApplication5.Employee("Zmztya",24);
                //打印本人信息
                emp.PrintInfo();
                emp[0] = "Father";
                emp[1] = "Mather";
                emp[2] = "Sister";
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine("家庭成员:{0}",emp[i]);
                }
                Console.WriteLine();
    
    
    结果: Name:Zmztya   Age:24
              家庭成员:father
              ....(省略其他四个,最后两个是空值)
     

    this 的关键字并没有发现其他特殊的用法。可以去掉除索引的意外的this,效果是一样的。

  • 相关阅读:
    promethus监控JVM jar包
    ubuntu中文乱码解决办法
    IT焦躁中的赤子青年
    ftp neo4j http kafka搭建
    查看python脚本执行过程
    解决coredns-7f9c544f75-2sjs2一直处于ContainerCreating
    番茄工作法
    数据库的性能优化
    MyBatis
    CentOS下安装JDK,Tomcat,Redis,Mysql,及项目发布
  • 原文地址:https://www.cnblogs.com/zmztya/p/5776256.html
Copyright © 2020-2023  润新知