Common .NET Gotchas

Matt Eland on September 05, 2019

.NET Development and C# in particular have come a long way over the years. I've been using it since beta 2 in 2001 and I love it, but there are som... [Read Full]
markdown guide

This is a really useful article— I learned a couple new things!

It got me thinking about a couple others off the top of my head...

1) Junior Devs seemingly are never taught how to correctly encode strings for URLs, Json, XML, etc (spoiler, string concatenation is not reliable for any of these).

2) On the topic of correctly handling IDisposables such as Streams....a byte[] should be favored over a MemoryStream when passing around data among functions, so that Streams are always used locally in the method that needs it and disposed of immediately (eg using{})...And while on this note, that bright idea that Dev for my client had about converting that byte[] to a base64 encoded string to pass around between functions —which then decodes it over and over— was a pretty bad idea!

3) Junior Devs need stop abusing ORM libraries...yeah occasionally it helps develop slightly quicker in small apps, but tightly coupling all Models with Table names and column names is the first yellow flag for enterprise apps. But the performance issue with in-code-looping-joins is beyond aweful....just learn Sql, create some views, and let Sql Server out of the ORM cage (sql server performance can be blazingly fast if you do it right from the beginning).


I love all of these. Thank you for sharing them. I also should have included something on storing dates in UTC. That's more data-model related, but it's another very common mistake I see alongside encoding.


It seems like a good idea to ensure streams are disposed of during functions, but I can't put my finger on exactly why - would it be possible to elaborate?

P.S. good additions.


Fantastic post Matt - really enjoyed it! with goldies like

Double.Epsilon and "Preprocessor statements are just plain old bad."


They're evil. It's like GOTO was when I was a kid - it just makes it so easy for programmers to do things they really shouldn't. Thanks for the feedback. I've given a training presentation internally to my company on this topic and found it valuable enough to share some concepts from that with the industry as a whole.


I have worked a lot with immutable DateTime type; however, your point on it was really amazing and helpful for me. Thanks!


Excellent post with important topics that need to be obeyed when building an API!


Great article! I learned a lot specially the "Double Comparison" which is completely new to me. Looks like StackOverflow has been hiding this for a very long time now. 🤣👍

code of conduct - report abuse