简体   繁体   English

在C#中,“ this”分配的示例是什么?

[英]What is an example of “this” assignment in C#?

Does anybody have useful example of this assignment inside a C# method? 有人在C#方法中有this分配的有用示例吗? I have been asked for it once during job interview, and I am still interested in answer myself. 面试时曾有人要求我这样做,但我仍然有兴趣回答自己。

The other answers are incorrect when they say you cannot assign to 'this'. 当其他答案说您不能分配给“ this”时,其他答案是错误的。 True, you can't for a class type, but you can for a struct type: 是的,您不能使用类类型,但是可以使用结构类型:

public struct MyValueType
{
    public int Id;
    public void Swap(ref MyValueType other)
    {
        MyValueType temp = this;
        this = other;
        other = temp;
    }
}

At any point a struct can alter itself by assigning to 'this' like so. 在任何时候,结构都可以通过分配“ this”来改变自身,就像这样。

using the this keyword ensures that only variables and methods scoped in the current type are accessed. 使用this关键字可确保仅访问当前类型范围内的变量和方法。 This can be used when you have a naming conflict between a field/property and a local variable or method parameter. 当字段/属性与局部变量或方法参数之间存在命名冲突时,可以使用此方法。

Typically used in constructors: 通常在构造函数中使用:

private readonly IProvider provider;
public MyClass(IProvider provider)
{
  this.provider = provider;
}

In this example we assign the parameter provider to the private field provider. 在此示例中,我们将参数提供程序分配给私有字段提供程序。

I know this question has long been answered and discussion has stopped, but here's a case I didn't see mentioned anywhere on the interwebs and thought it may be useful to share here. 我知道这个问题早已得到解答,讨论已经停止,但是在这种情况下,我在互联网上的任何地方都没有看到任何提及,并且认为在此处分享可能很有用。

I've used this to maintain immutability of members while still supporting serialization. 我使用它来维护成员的不变性,同时仍支持序列化。 Consider a struct defined like this: 考虑这样定义的struct

public struct SampleStruct : IXmlSerializable
{
    private readonly int _data;

    public int Data { get { return _data; } }

    public SampleStruct(int data)
    {
         _data = data;
    }

    #region IXmlSerializableMembers

    public XmlSchema GetSchema() { return null; }

    public void ReadXml(XmlReader reader)
    {
        this = new SampleStruct(int.Parse(reader.ReadString()));
    }

    public void WriteXml(XmlWriter writer
    {
        writer.WriteString(data.ToString());
    }

    #endregion
}

Since we're allowed to overwrite this , we can maintain the immutability of _data held within a single instance. 由于我们可以覆盖this ,因此我们可以保持单个实例中_data的不变性。 This has the added benefit of when deserializing new values you're guaranteed a fresh instance, which is sometimes a nice guarantee! 反序列化新值时,可以确保获得一个新实例,这有时是一个很好的保证! } }

only correct place for this from syntax point of view, is Extension methods in C# 3.0 when you specify first parameter of method as foo(ftype this, ...). 从语法角度来看,唯一正确的位置是将方法的第一个参数指定为foo(ftype this,...)时C#3.0中的扩展方法。 and then can use this extension for any instance of ftype. 然后可以将此扩展名用于ftype的任何实例。 But is's just syntax and not real this ovveride operation. 但是,这只是语法而不是真正的此ovveride操作。

if you're asked to assign something to this , there's quite a few examples. 如果你要求分配的东西这个 ,有相当多的例子。 One that comes to mind is telling a control who his daddy is: 我想到的是告诉控制对象他的父亲是谁:

class frmMain
{
    void InitializeComponents()
    {
        btnOK = new Button();
        btnOK.Parent = this;
    }
}

You cannot overwrite "this". 您不能覆盖“此”。 It points to the current object instance. 它指向当前的对象实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM