Michal Strehovský Profile picture
Oct 13, 2019 5 tweets 2 min read Read on X
1/5 A little know C#/.NET fact is that one can end up in a situation where `this is null` evaluates to true. One just needs a bit of reflection. Image
2/5 There's good reasons why it's little known. For one, it's kind of useless. The other reason is that doing that starts breaking invariants. For example, C# assumes `this` is always non-null. Call to `this.AndAgain()` should throw, but it won't. ImageImage
3/5 It also breaks invariants within the runtime. This snippet will work as expected, no surprises: Image
4/5 But this one will hit a `NullReferenceException` in a spot that the debugger cannot figure out. It has to do with how generic code over reference types gets shared within the runtime. ImageImage
5/5 .NET Native has an optimization that eliminates the entire method bodies on types that were never seen as allocated that also doesn't play well with this. This makes a good party trick (if the parties you go to are like the parties I go to), but don't do this in real code

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Michal Strehovský

Michal Strehovský Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @MStrehovsky

Sep 9, 2022
1/N One of my favorite optimizations in the upcoming .NET 7 Native AOT is static data preinitialization. This program prints a circle to the screen. Would you believe me if I said there is no code to compute the circle bytes in the NativeAOT'd executable file of this program?👇🧵 class Program {     static readonly byte[] s_bytes = Compute
2/N Main difference between Native AOT and the other forms of AOT in .NET is that Native AOT doesn't just pregenerate code - it generates all data structures necessary to run the code, including structures that describe layout of types in memory necessary to do things like run GC
3/N If we're generating data structures for GC, it's not a huge leap to generate "allocated object instances" as well, so we do that too. We also have an IL interpreter that can interpret static constructors. github.com/dotnet/corert/…
Read 4 tweets
Mar 30, 2020
1/N I thought it might be interesting to look at how generics are compiled to native code in .NET. A thread.
2/N When generic code is stored in IL on disk (as produced by e.g. the C#/VB/F# compiler), single generic method has a single method body. This method body is only runnable on the abstract CPU that the .NET compilers target. To run it on real hardware, it has to be translated.
3/N Real CPUs have pesky details that the abstract virtual machine doesn't care about - like registers and calling conventions.
Read 12 tweets
Jan 19, 2020
1/n It has always bugged me that I can't run my 64-bit C# games on MS-DOS. Today I fixed that. A thread.
2/n Windows EXE files consist of two parts: a DOS program that prints "This program cannot be run in DOS mode", followed by a header that Windows understands.
3/n In a way, all EXEs are DOS programs - they're just not very useful DOS programs. One day I found this project (that deserves way more GitHub stars than it has): github.com/Baron-von-Ried…
Read 10 tweets
Jan 9, 2020
1/7 Did you ever need to run a piece of C# code on Windows 3.11? Me neither, but I did it anyway. A thread.
2/7 The key ingredient it setting your expectations low. This is the program I got running:
3/7 I started where my "8 kB selfcontained game in C#" exercise finished: with a way to build fully native C# executable with no dependencies:
Read 7 tweets
Jan 8, 2020
1/5 How about a 2,121 byte version of the C# game? Still 100% self-contained. Here's what I did:
2/5 Implement enough x86 Windows support in CoreRT so that we can build the game for it (previous attempt was x64; but the x86 CPU instruction set is more compact): github.com/dotnet/corert/…
3/5 Get rid of string literals in the game and get rid of class constructors. I put those changes in a branch because I'm not so proud of it (just don't want to spend more than a day on this): github.com/MichalStrehovs…
Read 5 tweets
Dec 2, 2019
1/15 When compiling .NET ahead of time, the hardest question to answer is what code to compile.
2/15 For many non-.NET languages it's enough to compile the closure of Main(): start with Main() and compile all methods called from there transitively.
3/15 But .NET offers unconstrained reflection: one can `Type.GetType` a text string literally downloaded from the internet and call methods on that type
Read 15 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(