代码之家  ›  专栏  ›  技术社区  ›  Jonas Raphael Schultheiss

你需要“这个”做什么c#[重复]

  •  -1
  • Jonas Raphael Schultheiss  · 技术社区  · 7 年前

    两周前,我开始编写面向对象的代码,但仍然不知道它在使用什么。

    3 回复  |  直到 7 年前
        1
  •  2
  •   LukáÅ¡ Lánský    7 年前

    你需要先了解什么是实例。假设您有一个对象:

    public class House
    {
        public decimal Height { get; set; }
    }
    

    您可以有多个实例:

    var smallHouse = new House { Height = 100M };
    var bigHouse = new House { Height = 300M };
    

    每个实例都有自己的值 Height . 当你想与 身高 以一种方法 House ,您需要参考 现在的

    这可以通过使用 this 作为引用当前实例的特殊变量:

    public class House
    {
        public decimal Height { get; set; }
    
        public bool IsItTooBig()
        {
            return this.Height > 200;
        }
    }
    

    或者你可以省略 让C#猜测您的意思是实例值:

    public class House
    {
        public decimal Height { get; set; }
    
        public bool IsItTooBig()
        {
            return Height > 200;
        }
    }
    

    程序员对在那里显式表达的好坏有不同的看法。如果遵循大小写约定,则可以通过它区分实例状态和方法范围状态(普通变量)。

    在某些情况下,您绝对需要它,例如当您有命名冲突时,或者当您想从方法返回当前实例时:

    public class House
    {
        public decimal Height { get; set; }
    
        public House AddFloor()
        {
            Height += 100;
            return this;
        }
    }
    

    不过,您应该考虑在许多情况下应用不变性。

        2
  •  0
  •   Fandermill    7 年前

    关键字“this”表示用于显式调用该实例的方法、字段或属性的对象实例。

    当私有字段与给定方法中的参数同名时,通常使用:

    private string name;
    
    public void SetName(string name) {
        this.name = name;
    }
    
        3
  •  0
  •   M.kazem Akhgary    7 年前

    当您想要引用您使用的类中的实例字段时 this

    public class InstanceClass
    {
        int field = 10;
        public void Method()
        {
            int field = 0;
    
            Console.WriteLine(field); //      outputs 0
            Console.WriteLine(this.field); // outputs 10 because "this" refers to field.
        }
    }
    

    如果没有声明的局部变量与字段名冲突,则可以省略“this”。

    public class InstanceClass
    {
        int _field = 10;
        public void Method()
        {
            int field = 0;
    
            Console.WriteLine(field); 
            Console.WriteLine(_field); // prefixed with _.  
                                       // no conflicts
                                       // so "this" can be omitted.
        }
    }
    

    另一种不能忽略的情况是,当您使用索引器时。

    public class InstanceClass
    {
        private List<int> _source;
        private int offset;
    
        public int this[int index] // you use "this"
        {
            get => _source[index + offset]
            set => _source[index + offset] = value;
        }
    
        public void Method()
        {
            var first = this[0]; // must use "this" to refer to indexer for this class.
        }
    }
    

    “this”也用于调用构造函数重载。

    public class Foo
    {
        public Foo() : this(0) 
        {
            Console.WriteLine("world");
        }
    
        public Foo(int param1)
        {
            Console.WriteLine("Hello");
        }
    }
    
    //...
    
    var foo = new Foo(); // outputs "Hello world"
    

    “this”也指类本身的实例。所以,如果你想返回self的实例,你可以使用这个。

    public class Foo
    {
        public Foo ReturnMe() // weird example.
        {
            return this;
        }
    }