<p>Does anybody have useful example of <code>this</code> assignment inside a C# method? I have been asked for it once during job interview, and I am still interested in answer myself. </p>

[{'answer_id': 69851, 'author': 'Brad Wilson', 'author_id': 1554, 'author_profile': 'https://Stackoverflow.com/users/1554', 'pm_score': -1, 'selected': False, 'text': '<p>You cannot overwrite "this". It points to the current object instance.</p>\n'}, {'answer_id': 69878, 'author': 'dimarzionist', 'author_id': 10778, 'author_profile': 'https://Stackoverflow.com/users/10778', 'pm_score': -1, 'selected': False, 'text': "<p>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, ...). and then can use this extension for any instance of ftype. But is's just syntax and not real this ovveride operation.</p>\n"}, {'answer_id': 69880, 'author': 'Kimoz', 'author_id': 7753, 'author_profile': 'https://Stackoverflow.com/users/7753', 'pm_score': 0, 'selected': False, 'text': '<p>using the <strong>this</strong> keyword ensures that only variables and methods scoped in the current type are accessed. This can be used when you have a naming conflict between a field/property and a local variable or method parameter.</p>\n\n<p>Typically used in constructors:</p>\n\n<pre><code>private readonly IProvider provider;\npublic MyClass(IProvider provider)\n{\n  this.provider = provider;\n}\n</code></pre>\n\n<p>In this example we assign the parameter provider to the private field provider.</p>\n'}, {'answer_id': 69910, 'author': 'LohanJ', 'author_id': 11286, 'author_profile': 'https://Stackoverflow.com/users/11286', 'pm_score': -1, 'selected': False, 'text': "<p>if you're asked to assign something to <em>this</em>, there's quite a few examples. One that comes to mind is telling a control who his daddy is:</p>\n\n<pre><code>class frmMain\n{\n    void InitializeComponents()\n    {\n        btnOK = new Button();\n        btnOK.Parent = this;\n    }\n}\n</code></pre>\n"}, {'answer_id': 69988, 'author': 'ZeroBugBounce', 'author_id': 11314, 'author_profile': 'https://Stackoverflow.com/users/11314', 'pm_score': 7, 'selected': True, 'text': "<p>The other answers are incorrect when they say you cannot assign to 'this'.  True, you can't for a class type, but you <em>can</em> for a struct type:</p>\n\n<pre><code>public struct MyValueType\n{\n    public int Id;\n    public void Swap(ref MyValueType other)\n    {\n        MyValueType temp = this;\n        this = other;\n        other = temp;\n    }\n}\n</code></pre>\n\n<p>At any point a struct can alter itself by assigning to 'this' like so.</p>\n"}, {'answer_id': 23914965, 'author': 'mhand', 'author_id': 1582922, 'author_profile': 'https://Stackoverflow.com/users/1582922', 'pm_score': -1, 'selected': False, 'text': "<p>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.</p>\n\n<p>I've used this to maintain immutability of members while still supporting serialization. Consider a <code>struct</code> defined like this:</p>\n\n<pre><code>public struct SampleStruct : IXmlSerializable\n{\n    private readonly int _data;\n\n    public int Data { get { return _data; } }\n\n    public SampleStruct(int data)\n    {\n         _data = data;\n    }\n\n    #region IXmlSerializableMembers\n\n    public XmlSchema GetSchema() { return null; }\n\n    public void ReadXml(XmlReader reader)\n    {\n        this = new SampleStruct(int.Parse(reader.ReadString()));\n    }\n\n    public void WriteXml(XmlWriter writer\n    {\n        writer.WriteString(data.ToString());\n    }\n\n    #endregion\n}\n</code></pre>\n\n<p>Since we're allowed to overwrite <code>this</code>, we can maintain the immutability of <code>_data</code> held within a single instance.  This has the added benefit of when deserializing new values you're guaranteed a fresh instance, which is sometimes a nice guarantee!\n    }</p>\n"}]
['https://Stackoverflow.com/questions/69843', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11256/']