58

How many keywords I can fit into a single C# expression?

 5 years ago
source link: https://www.tuicool.com/articles/hit/QFNZfaj
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

As I was playing few days ago with yield return await I got an idea to try fit as much “keywords” as possible into one “expression”. I mean as much blue words (in Visual Studio’s default colors) as possible together (not separated by semicolon, comma, colon, bracket, etc.).

Obviously yield return await gives us 3 together. One can also do yield return this , yield return base or yield return sizeof(...) . But that’s still just 3. Can I do more? While I was waiting for my car on a yearly checkup, hence I was not mindlessly wasting time, I came up with 7.

class C
{
	async Task Test()
	{
		switch (this)
		{
			case null when await this is false:
				break;
		}
	}

	public System.Runtime.CompilerServices.TaskAwaiter<bool?> GetAwaiter() => default;
}

The case null when await this is false is the answer. At this point I ran out of time as I received a message from garage that my car is ready. Maybe you can come up with even longer solution and I’m eager to see it.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK