tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Friday’s This<T>

25 Jul 2019 C#

Last week on Friday I was working on some code, I don’t even remember what it was, and there was excessive usage of this. And my personal pet peeve is writing most readable code in most succinct way possible. For sure this hurts this (no pun intended). The following class is a result of that Friday’s frustration.

I wrote it in few minutes, just for fun. I don’t claim it’s correct or useful.

Oh, and yes. The Τhis property is (ab)using Unicode because it is using U+03A4 for T.

public class This<T> where T : class
{
	public T Τhis { get; }

	public This(T value) => Τhis = value;

	public override int GetHashCode() => Τhis?.GetHashCode() ?? 0;

	public override string ToString() => Τhis?.ToString();

	public override bool Equals(object obj) => Τhis is null ? obj is null : Τhis.Equals(obj);

	public static implicit operator T(This<T> @this) => @this.Τhis;

	public static explicit operator This<T>(T value) => new This<T>(value);
}

One interesting thing from this exercise – I realized I can use expression body for constructor.

Profile Picture Jiří Činčura is an independent developer focusing on data and business layers, language constructs, parallelism and databases. Specifically Entity Framework, asynchronous and parallel programming, cloud and Azure. He's Microsoft Most Valuable Professional and you can read his articles, guides, tips and tricks at www.tabsoverspaces.com.