The impact of SqlDataReader.GetOrdinal on performance

I recently had a discussion about the impact of SqlDataReader.GetOrdinal on execution of a SqlClient.SqlCommand. I then decided to run some code to measure the difference, because I think that’s the only way to get a decent opinion. This is the code that I’ve used to run a certain query 1000 times: private void InvokeQuery(Action mapObject) […]

TransactionScope & SqlConnection not rolling back? Here’s why…

A while back we ran into an issue with one of our projects where we executed a erroneous query (missing DELETE statement), and then left the database in an inconsistent state. Which is weird, considering the fact that we use a TransactionScope. After some digging around I found the behavior I wanted, and how to […]

About a dictionary, removing and adding items, and their order.

I had a weird problem today using a Dictionary. The process involved removing and adding data, and then printing the data. I assumed that it was ordered. I was wrong! Let me show you: var dictionary = new Dictionary<int, string>(); dictionary.Add(5, “The”); dictionary.Add(7, “quick”); dictionary.Add(31, “brown”); dictionary.Add(145, “fox”); dictionary.Remove(7); // remove the “quick” entry After a […]

Foreach now captures variables! (Access to modified closure)

Foreach has changed in C# 5.0! Consider the following piece of code in C# < 5.0: public class Test { public static void Main() { var words = new[] { “foo”, “bar”, “baz”, “beer” }; var actions = new List<Action>(); foreach (string word in words) { actions.Add(() => Console.WriteLine(word)); } actions.ForEach(e => e()); } } […]

When using an enum in PowerShell, use the member’s name, not the member’s value

Consider the following enum in C#: enum State { Started, Stopped, Unknown } Note that I have not added an explicit value for the enum members. They will be generated by the compiler. As stated in the C# spec: … its associated value is set implicitly, as follows: If the enum member is the first […]

WebClient not sending credentials? Here’s why!

TL;DR version here. This post applies to more than just GitHub, read the rest to see the behavior! I was playing with the GitHub API (more specifically generating a new OAuth token). So what you need to do, as per the documentation, is to post a certain JSON string to https://api.github.com/authorizations, with Basic Authentication. I’m […]

Unless you have a VERY good reason, rethrow your exception.

I see it a lot, people write a function, they catch a possible exception and throw one of their own OmgSomethingHorribleHappenedException(); Check this simplefied example: private static void Main() { try { var result = (new Api()).Find(“foo”); Process(result); } catch (Exception e) { Console.WriteLine(e.Message + Environment.NewLine + e.StackTrace); Console.Read(); } } And the Api.Find(string toFind) […]