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: [EditorBrowsable(EditorBrowsableState.Never)] [System.Obsolete(“use new method abc instead”)] public void OldMethodThatShouldNotBeUsedAnymore() { // Do some stuff }

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: Console.WriteLine(“WPF Rendering ” + GetStringForRenderCapabilityTier(RenderCapability.Tier)); RenderCapability.TierChanged += (sender, args) => Console.WriteLine(“WPF Rendering changed to ” + […]

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 […]

C# Pitfalls

Closures If you have code like this: // strings is a list containing the values a, b and c foreach (string str in strings) { Button btn = getButton(str); btn.Click += new EventHandler(delegate { MessageBox.Show(str); }); } The result will be that each button shows a Messagebox with c. Altough one would expect to see […]

Disable validation of Self-Signed Certificates

You can use the following code: ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true; 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 […]

C# Entity Framework – Clear Context

Put the following code in the partial class of your entity context: public partial class MyEntities { public IEnumerable GetAllObjectStateEntries() { return ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged); } public void ClearEntities() { foreach (var objectStateEntry in GetAllObjectStateEntries()) { Detach(objectStateEntry.Entity); } } } If you want to clear the context you just call the […]

C# Dijkstra

Implementation Uses the HeapPriorityQueue.Code on Github // // Copyright 2012 Patrick Uhlmann // // Licensed under the Apache License, Version 2.0 (the “License”); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable […]