~/devreads

#evil code

15 posts

25 May 2019

jonskeet 3 min read

This morning I tweeted this: Just found a C# 8 nullable reference types warning in Noda Time. Fixing it by changing Foo(x, x?.Bar) to Foo(x, x?.Bar!) which looks really dodgy… anyone want to guess why it’s okay? This attracted more interest than I expected, so I thought I’d blog about it. First let’s unpack what … Continue reading Lying to…

c#c# 8evil code

13 Apr 2018

jonskeet 8 min read

I started writing a blog post about versioning in July 2017. I’ve mostly abandoned it, because I think the topic is too vast for a single post. It potentially needs a whole site/wiki/repository devoted to it. I hope to come back to it at some point, because I believe this is a hugely important topic … Continue reading Backward compatibility…

c#evil code

2 Mar 2018

jonskeet 5 min read

Background There are three things you need to know to start with: Operations on read-only variables which are value types copy the variable value first. I’ve written about this before on this blog. C# 7.2 addresses this by introducing the readonly modifier for structs. See the language proposal for more details. I was touched to … Continue reading Implementing IXmlSerializable…

c#c# 7evil codenoda timeperformance

26 Apr 2017

jonskeet 2 min read

This is a brief post documenting a very weird thing I partly came up with on Stack Overflow today. The context is this question. But to skip to the shock, we end up with code like this: That just shouldn’t happen. You shouldn’t be able to create an instance of an open type – a … Continue reading Surprise! Creating…

c#evil codestack overflow

8 Jan 2016

jonskeet 2 min read

Today I’ve been reviewing the ECMA-334 C# specification, and in particular the section about class instance constructors. I was struck by this piece in a clause about default constructors: If a class contains no instance constructor declarations, a default instance constructor is automatically provided. That default constructor simply invokes the parameterless constructor of the direct … Continue reading To base()…

c#evil code

27 Jul 2015

jonskeet 4 min read

First note: this blog post is very much tongue in cheek. I’m not actually planning on using the idea. But it was too fun not to share. As anyone following my activity on GitHub may be aware, I’ve been quite a lot of work on Protocol Buffers recently – in particular, a mostly-new port for … Continue reading “Sideways overriding”…

c#evil code

3 Jun 2015

jonskeet 7 min read

At the moment, I’m spending a fair amount of time thinking about a new version of the C# API and codegen for Protocol Buffers, as well as other APIs for interacting with Google services. While that’s the context for this post, I want to make it very clear that this is still a personal post, … Continue reading Backwards compatibility…

c#evil code

1 Dec 2014

jonskeet 8 min read

Here’s a few things you may not be aware of: C# identifiers can include Unicode escape sequences (\u1234 etc) C# identifiers can include Unicode characters in the category “Other, formatting” (Cf) but these are ignored when comparing identifiers for equality The Mongolian Vowel Separator (U+180E) has oscillated between the Cf and Zs categories a couple … Continue reading When is…

c#evil code

7 Nov 2014

jonskeet 7 min read

When is a string not a string? As part of my “work” on the ECMA-334 TC49-TG2 technical group, standardizing C# 5 (which will probably be completed long after C# 6 is out… but it’s a start!) I’ve had the pleasure of being exposed to some of the interesting ways in which Vladimir Reshetnikov has tortured … Continue reading When is…

c#evil code

23 Oct 2014

jonskeet 7 min read

For a while now, I’ve been a big fan of a pattern in C# which mimics Java enums to a certain extent. In general, it’s a lovely pattern. Only after reading a comment on a recent blog post by Eric Lippert did I find out about a horrible flaw. Dubious thanks to John Payson for … Continue reading Violating the…

c#evil code

22 Aug 2014

jonskeet 9 min read

A comment on a Stack Overflow post recently got me delving into constants a bit more thoroughly than I have done before. Const fields I’ve been aware for a while that although you can specify decimal field as a const in C#, it’s not really const as far as the CLR is concerned. Let’s consider … Continue reading When is…

c#evil codecsharpevilcode

8 Aug 2014

jonskeet 3 min read

I started writing a post like this a long time ago, but somehow never finished it. Countless posts on Stack Overflow are vulnerable to SQL injection attacks. Along with several other users, I always raise this when it shows up – this is something that really just shouldn’t happen these days. It’s a well-understood issue,and … Continue reading The BobbyTables…

c#evil codestack overflow

16 Jul 2014

jonskeet 7 min read

Introduction Recently I’ve been optimizing the heck out of Noda Time. Most of the time this has been a case of the normal measurement, find bottlenecks, carefully analyse them, lather, rinse, repeat. Yesterday I had a hunch about a particular cost, and decided to experiment… leading to a surprising optimization. Noda Time’s core types are … Continue reading Micro-optimization: the…

c#evil codeperformance

8 Apr 2014

jonskeet 3 min read

This post is the answer to yesterday’s brainteaser. As a reminder, I was asking what purpose this code might have: public static class Extensions { public static void Add<T>(this ICollection<T> source, T item) { source.Add(item); } } There are plenty of answers, varying from completely incorrect (sorry!) to pretty much spot on. As many people … Continue reading Extension methods,…

c#c# 6evil code

7 Apr 2014

jonskeet 1 min read

Just a really quick one today… What’s the point of this code? Does it have any point at all? public static class Extensions { public static void Add<T>(this ICollection<T> source, T item) { source.Add(item); } } Bonus marks if you can work out what made me think about it. I suggest you ROT-13 answers to … Continue reading Quick brainteaser…

c#evil code