C# Generic Method Resolution Gotcha

Tuesday, August 19, 2014

Yesterday, I broke some unit tests after removing an unused type parameter from a method, which seemed like a small and harmless code modification. Well, it turns out that it isn’t. If you want to know why, keep reading. Consider the following code snippet: 1: 2: public class Tests 3: { 4: 5: public void Test() 6: { 7: Assert.AreEqual("Int", DoSomething(1));...
tags: , ,
no comments

CLR Memory Diagnostics Released

Saturday, May 11, 2013

Last week, Microsoft released the new CLR Memory Diagnostics (ClrMD) library, which is a set of APIs for programmatically inspecting a crash dump of a .NET program. To start playing with it, you first need to add the Microsoft.Diagnostics.Runtime package from NuGet (be sure to select Include Prerelease, because it is a prerelease version): You can use ClrMD to analyze a crash dump from disk, or to attach to a live process. In both scenarios, you will need to use one of the static factory methods declared in the DataTarget class. To analyze a...
no comments

Setting a WPF Window to be Always on Top

Saturday, January 5, 2013

Some applications require that their main window will always be on top of all the other windows. Doing so in WPF is quite simple. In this post I will show how. In order for the window to be opened on top of all the other windows, set its Topmost property to True. Doing just that is not enough, because when the window will lose its focus, it will be placed behind the focused window. To keep the window top-most even after it loses its focus, add an EventTrigger to the window's LostFocus event, and set the Topmost property...
tags: , , ,
no comments

Automatic Properties are not Fields

Friday, December 28, 2012

Even though that the statement in the title is trivial, the fact that the syntax of automatic properties and fields is almost identical can sometimes make us forget that they are two different things. Take, for example, the following struct: 1: 2: public struct MyData 3: { 4: public int A; 5: public int...
tags: , ,
no comments

Compiler Error: Invalid Character in the Given Encoding

Friday, December 14, 2012

Recently, a colleague of mine received the above compilation error, together with hundreds of other, less helpful errors, when he tried to compile what seemed to be a perfectly fine XAML code. The problems started after he removed all the comments from the source files with a program that he created. The program simply loads the source file's content, removes the comments, and then writes the updated content back to the file. Here is a naive example to illustrate the problem: 1: public void ProcessFile(string path) ...
tags: , ,
no comments

WPF Command Confirmation

Thursday, October 25, 2012

In the last post, I introduced you to my DynamicGridFormationBehavior, which gives you the ability to dynamically change a grid's formation, using only Xaml. In this post, I want to introduce you to another behavior, called ConfirmationBehavior. First let's set the context. Assume that we have an application that handles customers' information, and allows CRUD operations on it. The client asked that before any critical operation on the data (deleting a customer for example), a confirmation message will appear. If you're following the MVVM pattern in your WPF applications (and you should), you will probably end up...
tags: , , ,
no comments

Changing WPF Grid Structure Dynamically

Friday, October 19, 2012

In an application I was working on recently, I needed to implement a split screen, like those on the closed-circuit cameras monitors. Usually, a simple Grid or a UniformGrid would do the trick, but in this case, the requirement was to allow the user to dynamically change the grid's structure. The following screenshots shows the grid's structure when the user chooses to display two, three, or four screens, respectively: As you can see, different user selections requires entirely different grid structure, with different number of...
tags: , , ,
no comments

Intern Pool Improvements between Various .NET Framework versions

Monday, October 8, 2012

As you probably know, .NET supports string interning for better memory usage of .NET applications. String literals are automatically interned by the runtime, while any other string can be interned by an explicit call to String.Intern. I will not go into details regarding string interning, if you are not familiar with the concept, you can get started by reading the MSDN documentation for the String.Intern method. In this post, I would like to write about one of the implementation details of the .NET intern pool, specifically, where it is stored and how the storage strategy was changed between recent...
no comments