How to properly deprecate methods

If you work with a C# class that has many deprecated methods in they clutter intellisense and you cannot see the useful methods anymore. To avoid this you can add the Annotation EditorBrowsable and disable intellisense:

Find out how wpf is being rendered

WPF applications can render in three modes: everything in software, partial hardware rendering or full hardware rendering. The rendering mode may have a big effect to the application performance. This it is important to know which mode is active:

C# Pass Arguments to the first instance of a program

Sometimes you don’t want the user to run more than one instance of a program. But if the user associates a filetype with your programm it shall open the filetype in this first instance. The code below uses interprocess communication to find out if there is already another instance. It further allows the first instance to register an eventhandler which can be called from a second instance in order pass its command line parameters to the first instance.

This is how to use the code:

This is the SingleInstanceEnforcer class

C# Pitfalls

Closures

If you have code like this:

The result will be that each button shows a Messagebox with c. Altough one would expect to see a MessageBox with a for the first button. The reason is that the foreach loop gets unrolled into a while loop which has it’s str variable declared outside of the loop. The statement in the loop does not catch the value of the variable but the variable itself. After the foreach loop the value of the variable str is c.

See Blog-Post for details

Disable validation of Self-Signed Certificates

You can use the following code:

Be aware that this code does just accept every certificate and it is not only active for one request but for all requests. You should think before you use this code. But for development convenience or on a test system it might be ok to make your program easier to install and test.

C# Entity Framework – Clear Context

Put the following code in the partial class of your entity context:

If you want to clear the context you just call the ClearEntities() method on the reference to the context

But be aware that this code does set all foreign key references to null in all entities that have been retrieved from the context before the method was called. You should not use any entities of the context anymore when you call this code.

WPF: Check if there are validation errors

I worked on a wpf window containing some controls that were required and that validated the format of the entered data. Once the user pressed a button I wanted to check if there was any validation error and notify the user with an error message. This is how to do it:

Helper class:

Code of the button:

The code above assumes that there is a Container (for example a stack panel) containing all fields that need to be validated whose name is FieldHolder

Source: stackoverflow.com/questions/127477/detecting-wpf-validation-errors

C# Dijkstra

Implementation

Uses the HeapPriorityQueue.
Code on Github

Graph Classes

C# PriorityQueue with fast Update operation

Implementation

Uses a heap for fast retrieval of the smallest element as well as a dictionary for a fast access to an element which is already in the data strucutre. This priority queue is not too bad for implementing a Dijkstra (a fibonacci heap would be perfect…).
Code on Github

Helper Class

Test