The Weird Things Roslyn Gets up To

Ever wanted to see some strange, interesting, and weird .NET behaviour and features? What about bizarre C# to baffle? If so, then welcome to this little curated and peculiar collection.

The Weird Things Roslyn Gets up To
Let's go, don't wait, this night's almost over 🎵

We've all seen the JavaScript examples where NaN === NaN is false etc. If not, check out some examples in the repo below:

GitHub - denysdovhan/wtfjs: 🤪 A list of funny and tricky JavaScript examples
🤪 A list of funny and tricky JavaScript examples. Contribute to denysdovhan/wtfjs development by creating an account on GitHub.

Some you might've seen them, some might be new to you! But what Strange things can we dig up in .NET/C#? Here's a collection I've gathered over the course of 2022.

🤷‍♀️
Just in case: the gag in the title is that Roslyn is the name of the .NET compiler.

Smallest Valid C#

At least at the time of .NET 6:

{}

You can see for yourself in this Sharplab.io snippet.

Originally this came to me as a tweet from @Nick_Craver:

https://twitter.com/Nick_Craver/status/1512615440358551553

If you want to dig into it, there is a really fun post from @nietras1 exploring step by step what the smallest valid C# is.

World’s Smallest C# Program (featuring `N`)
TLDR: Tongue in cheek post on how - in .NET 5+ and C# 9+ - the smallest possible C# program appears to be {} or 2 characters long. This doesn’t do much, though. Using N (github, nuget)you can write a program doing something in 4 characters with e.g. N();in .NET 6 and C# 10. Along the way learn…

It's All Greek to Me

https://twitter.com/leppie/status/1492256050774487047

Excuse me? I'm extremely happy this came with a Sharplab snippet. Let's break it down. First, looking at the full original code:

using System;
unsafe class Program
{
    delegate void bar(int* i);
    static Index ƛ(bar β) => default;
    static void Main(string[] args)
    {
        int[] ω = { };
        int Ʃ = 42;
        int? Φ = 10;
        var ϼ = ω;
        
        ϼ=ω[ƛ(β:Δ=>Φ??=ω[^++Ʃ]/Ʃ|*&Δ[-0%Ʃ]>>1^Φ??0!&~(δ:Ʃ,^Φ..).δ)..(1_0>.0?Ʃ:0b1)];
    }
}

Then after we make some changes we can begin to more easily pull the example apart:

unsafe class Program
{
    delegate void DelegatePointer(int* i);
    static Index Indexer(DelegatePointer delegatePointer) => default;
    static void Main(string[] args)
    {
        int[] intArray = { };
        int position = 42;
        int? nullableValue = 10;
        var intArray2 = intArray;

        intArray2 = intArray[Indexer(
            delegatePointer: x => nullableValue ??= intArray[^++position] / position | *&x[-0 % position] >> 1 ^ nullableValue ?? 0! & ~(δ: position, ^nullableValue..).δ)..(1_0 > .0 ? position : 0b1)];
    }
}

Where we have:

  • A delegate that takes in a pointer
  • The newish Index type, introduced to help with Ranges.
  • A whole bunch of arrays and int types

Then we get into the big complex line, which is made up of:

  • Array indexing
  • The Index from earlier
  • Lambda expressions
  • A lot of pointer operators
  • And some ranges thrown in

So it's still hard to read, but unobscured by Greek and broken down to the sum of its parts it's a little more manageable and no less fun to show off to others!

Not as Much Greek

Found this in my feed:

https://twitter.com/damovisa/status/1539789609814462464

It's a less Greek version of what we just saw. If you zoom in, you can see it's also made up of patterns, and the new range work.

Tolstoy in C#

https://twitter.com/jaredpar/status/1529551649768886272

There's a lot more natural language in C# when compared to lower level languages, or even C# of a decade ago. Since C# 7 we've had Patterns which have been iterated on since meaning we can get to the fun state above.

Sure, there's a little cheating involved as they're not all keywords (check the Sharplab link in the tweet) but it's still fun to let it sink in after seeing it for the first time.

Structs Have a Fluid Identity

Being bored as a nerd, you're bound to be the stereotype at least once, and I went looking for interesting C# snippets and found that Structs can reassign their this value.

Full credit to Omer Mor for this StackOverflow answer:

What’s the strangest corner case you’ve seen in C# or .NET?
I collect a few corner cases and brain teasers and would always like to hear more. The page only really covers C# language bits and bobs, but I also find core .NET things interesting too. For examp...

Maybe there's a cool reason why, but it feels.. weird:

public struct Teaser
{
    public void Foo()
    {
        this = new Teaser();
    }
}

There's Never Enough Null Checks

This tweet right here:

https://twitter.com/davidfowl/status/1421712013936369665

Shows a wild amount of null checks. Whether you believe C# has too many ways to do the same thing or not, this amount is impressive. Though if you frame it so, you can have this as an advantage of C#, where your niche requirement is met by some obscure and valid operation.

In fact, this tweet pushed me to keep a collection of other interesting C# snippets, including the null checks above:

GitHub - nikouu/Neat-CSharp-Snippets: C# snippets that make me say “this will make a fine addition to my collection”.
C# snippets that make me say "this will make a fine addition to my collection". - GitHub - nikouu/Neat-CSharp-Snippets: C# snippets that make me say "this will make a fine addition t...

I did run across this meme that I feel has some truth for the future:

https://twitter.com/_josephwoodward/status/1566508508870213634

What the Compiler Doin'?

Maybe in the future I'll do a full write up exploring these but if you haven't seen, Bartosz Adamczewski does really fascinating writeups and showcasing of compiler manipulations in .NET. You can view all of them on his website or check out a few of his tweets here:

https://twitter.com/badamczewski01/status/1544749813740232705
https://twitter.com/badamczewski01/status/1544735892794998784
https://twitter.com/badamczewski01/status/1542799681717149697

To Conclude

Since getting deeper into the .NET community in 2022, I've been collecting little snippets of oddity and fun. I'm sure there's a lot more out there and maybe in the future I'll collect enough to write up another post. I hope you found this as enjoyable as I do!