Alex Meyer-Gleaves

Two simple tips for working with LINQ and IEnumerable<T>

on c#, LINQ

Meet the test subject Let’s create a simple class that returns the numbers from 1 to 100. public class Loopy { public int Enumerations { get; private set; } public IEnumerable<int> GetSome() { foreach (int number in Enumerable.Range(1, 100)) { Enumerations++; yield return number; } } } We use Enumerable.Range to grab the numbers from 1…

Getting the Windows Product ID using WMI

on c#, Windows, WMI

I needed to get the Windows Product ID from managed code recently and decided that using WMI would be the most reliable and stable way to get the job done. Do not follow any examples that show you how to get this information from the registry. You simply cannot rely on those registry keys being…

CodeDomProvider and Compiler Warning Levels

on c#, CodeDOM

When generating code using the CodeDomProvider you may noticed that by default you do not get the same warning messages in the CompilerResults that you receive when compiling in Visual Studio. For example, lets take the simple code below that has a static method with two unused variables. public class Test { static void testMethod() { int…

Attaching the Debugger only in Debug

on c#, visual studio

I noticed an article on the Infinite Codex blog that demonstrates how to debug CLR Stored Procedures. The example uses a #if preprocessor directive to compile the debugging code only if the DEBUG symbol is defined. Personally, I find using the #if directive makes your code look rather ugly, and accidentally including code inside the…

Compute any hash for any object in C#

on c#

Approach The .NET Framework already has many classes for cryptography in the System.Security.Cryptography namespace, so there is no need to worry about the hashing algorithms myself. The cryptography classes supplied by the .NET Framework expect to be given a byte array from which the hash can be computed. So, if I can create…