Use nameof() to set a string variable to its name

December 17, 2019 c#, nameof()

This is one of those things that should have just been obvious to me but once I saw it I wondered why I had never thought to use it myself. nameof() can be used when declaring a variable to set the value of the variable to the name of the variable:

public const string MyVariable1 = nameof(MyVariable1); public static void Main() { Console.WriteLine(MyVariable1); }
Read More

Dynamically loaded DLL thows AccessViolationException on first function call

October 22, 2019 c#, LoadLibrary, GetProcAddress

In my library GLESDotNet I load the libegl.dll and libglesv2.dll DLLs dynamically via the Win32 functions LoadLibrary and GetProcAddress. I already had everything working via DLLImport but I wanted to be able to load the DLLs from different subdirectories depending on the architecture.

For the first test I only loaded the libegl.dll DLL before moving on to the libglesv2.dll. I made the classic programmer mistake of changing too much at one time. The first call to any function in libegl.dll resulted in an AccessViolationException. I couldn't figure out what was going wrong so I decided to revert my changes and try again.

I noticed that when the DLLs were in the same directory the AccessViolationException went away. Changing the working directory to the directory of the DLLs also solved the problem once the DLLs were placed in subdirectories again. This led me to believe that loading libegl.dll must be implicitly loading libglesv2.dll. Loading libglesv2.dll via LoadLibrary along with libegl.dll solved the problem.

Abusing IDisposable

August 27, 2019 c#, IDisposable

In C# the interface IDisposable is usually used to indicate that an object needs to dispose of unmanaged resources. C# provides the using syntax to ensure that the Dispose method is called once the instance of the object is no longer needed.

Dispose is just like any other method though and the using syntax can be used quite nicely for other use cases as well. Let's create a class we can use to collect some actions to be "deferred" until a later time.

Read More

Generating string permutations for unit testing

February 11, 2019 c#, nunit, math

I needed to parse a string containing 6 characters. Each character should only be one of 3 possibilities: '?' for null, '0' for false or '1' for true. The problem sounded easy enough to generate a whole bunch of unit tests for.

Read More

Functional Card Deck in C#

October 11, 2018 c#, fp

I've been inspired lately by Mark Seemann's series of posts about Applicative Functors. One of the latest posts is an example about creating a full deck of cards. Most of posts up to this point have contained a C# example but for some reason this one didn't. This inspired me to take a shot at it.

Read More