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

- 7,960
- 5
- 61
- 99

- 33,874
- 33
- 95
- 118
6 Answers
The other answers are incorrect when they say you cannot assign to '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.

- 3,652
- 3
- 31
- 40
-
2Ohhh it's because as a struct it is a value type and the assignment operator = with value types copies values. – Brian Leahy Sep 16 '08 at 07:44
-
Enlightening! Subjective Comment: however apart from the Swap(ref struct) scenario, I can't think of anything else that can benefit from this. this cannot be used before assigning all members of that struct. I'd rather use the s2 = s1 instead of having a method with this used as above. – Gishu Sep 16 '08 at 09:02
-
Also have not seen this before :) – leppie Oct 07 '08 at 08:09
-
With C#, you never have to wonder why you love it - it's always reminding you. – rookie1024 Jul 07 '16 at 15:52
using the this 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.
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.

- 375
- 4
- 11
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:
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 has the added benefit of when deserializing new values you're guaranteed a fresh instance, which is sometimes a nice guarantee!
}

- 1,231
- 1
- 11
- 21
-
You've just created a mutable value type. And worse still, you create a mutable value type that *appears* as if it's immutable at first glance. This is all kinds of evil. – Servy May 28 '14 at 14:54
-
I would recommend explicitly implementing the IXmlSerializable interface for a little more safety, but then the question becomes, is it ever OK to have a serializable struct, since in order to be serializable you need to be mutable in some sense. – mhand Jun 06 '14 at 16:02
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.

- 18,517
- 4
- 22
- 23
-
Not true. Values types can assign to `this` and there is no other syntax for it. – jnm2 Jan 08 '16 at 18:03
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;
}
}

- 45
- 1
You cannot overwrite "this". It points to the current object instance.

- 67,914
- 9
- 74
- 83