Blog: C#

My most used Visual Studio and ReSharper extensions

March 12, 2015, (updated on August 25, 2015), 5 comments, Software Development

This article describes some of the Visual Studio and ReSharper extensions and development tools I’m using to enhance my daily C#/.NET development productivity. ReSharper extensions In my opinion, ReSharper is a must have extension for Visual Studio which provides lots of improvements, for example better refactorings, an improved code snippet and template engine, additional static code analysis and much more. My personal configuration: Use camel humps instead of whole word when navigating or deleting: ReSharper > Options… > Environment > Editor > Editor behavior > Use CamelHumps Copy XML documentation from interface when generating interface methods: ReSharper > Options… > […] Read more...

Tags: , , , , , ,

Cheat Sheet: Best practices for writing XML documentation in C#

March 11, 2015, (updated on October 4, 2018), 3 comments, Software Development

The XML documentation tags of C# are described very well in the MSDN. However, the article does not explain how the contained phrases and sentences should be written. This article tries to fill this gap by providing rules and some sample phrases. I recommend using StyleCop because its rules enforce some of the XML documentation recommendations from this article. Also check out the Visual Studio extension GhostDoc which automates and simplifies the writing of XML documentation. General All XML documentation phrases should end with a period (.) and no blank: <summary>Represents an XML document.</summary> public class XDocument { The summary […] Read more...

Tags: , , , , , ,

Elegant method parameter validation with Code Contracts support

February 25, 2015, (updated on February 26, 2015), 1 comment, Software Development

Each C# developer knows the drill: Each method parameter has to be validated against null values, wrong value ranges or other contract constraints. When also validating using Code Contracts, the resulting code may take up most of the method body: public void Foo(object a, string b) { Contract.Requires(a != null); Contract.Requires(b != null); Contract.Requires(b != string.Empty); if (a != null) throw new ArgumentNullException("a"); if (string.IsNullOrEmpty(b)) throw new ArgumentNullException("b"); ... } Note: When Code Contracts runtime validation is enabled, the call to Requires may throw an exception and the own code for throwing an ArgumentNullException is not needed. However, often the […] Read more...

Tags: , , , , , ,

Recommendations and best practices for implementing MVVM and XAML/.NET applications

January 30, 2015, (updated on June 15, 2018), 36 comments, Software Development

In this article I’ll describe the rules and practices I’m following for XAML and MVVM application development. I’m using the described techniques since multiple years and they have proven themselves for me. Most of the described rules apply for all types of XAML projects – Windows Store, Windows Phone, Silverlight and WPF projects. Sample code: The source code of the application Visual JSON Editor contains sample implementations for most of the discussed recommendations. Some sample code snippets in this article use the MVVM classes from the MyToolkit library which is developed by myself. Project file structure All MVVM/XAML applications should […] Read more...

Tags: , , , , , , ,

How to mimic singlecast events in C#

January 12, 2015, (updated on February 13, 2015), Software Development

C# only supports multicast events and delegates. Even if you define a delegate property without the event keyword, it still remains multicasted: public class Foo { public Action MyEvent { get; set; } } var foo = new Foo(); foo.MyEvent += delegate { }; foo.MyEvent += delegate { }; The only difference between a regular property with a delegate type and an event property are: The event property can only be invoked by its class, the delegate in the regular property can also be invoked from outside (which may not be desired) Accessing and assigning an event property is only […] Read more...

Tags: , , ,

Implementation of “hybrid” (console or GUI) .NET applicatios

December 29, 2014, (updated on October 19, 2015), 2 comments, Software Development

Have you ever had the need to use an application (.exe) as a command line tool when started in the console (cmd.exe) and as GUI application when started from the Explorer shell? This article explains a possible implementation for this requirement. To be clear: I didn’t find the perfect solution; a Windows .NET application is either a console application or a Windows application. Either type can be used in the opposite mode, but both variants have some drawbacks. This article describes the solution which I think has the least disadvantages. The solution works by defining the application as console application […] Read more...

Tags: , , , ,

The correct handling of a WCF client’s life cycle

December 21, 2014, (updated on February 25, 2015), Software Development

The most intuitive way of handling a generated WCF client’s life cycle is by putting it into a using block: using (var svc = new MyServiceClient()) { var result = await svc.SumAsync(1, 2); } Note: You should not use static or singleton client objects. However, the problem with this code is, that IDisposable is not implemented correctly in the base class ClientBase. The problem is that the Dispose method always calls Close but Close may throw an exception. If this exception is thrown an additional call to Abort is needed but which is not made. Missing this Abort call may […] Read more...

Tags: , , ,

How to create an MSI installer using the WiX toolset

November 25, 2014, (updated on December 20, 2014), Software Development

This article provides a simple guide about how to quickly create a WiX installer project for an existing WPF application or another Windows desktop application. Continue reading at my company’s blog… Read more...

Tags: , , , , ,

Write detailed C# XML documentation and still keep the source code clean

October 20, 2014, (updated on January 12, 2015), Software Development

For my open-source project MyToolkit I write a lot of documentation. Currently the documentation for a class has to be updated in two locations: The source code and the project’s wiki. I asked myself how to avoid this and document everything in the source code file – while keeping the source code simple by not putting too much documentation in it. To write detailed documentation in your C# code, you can use the tags <remarks> and <example> (<summary> should always be there). To avoid polluting the source code file with documentation, you can additionally use the <include> tag to “outsource” […] Read more...

Tags: , , ,

Use JSON files with schema for application configuration

September 12, 2014, (updated on February 25, 2017), 2 comments, Software Development

Often, we face the problem that our customers need highly configurable applications but there is no time to develop rich configuration GUIs. This article describes a possible solution which is a compromise between editing raw configuration files and implementing rich configuration GUIs. The idea: First, implement some simple POCO classes (Plain Old CLR Object) representing the structure of the required configuration. Using a JSON serializer these classes can then be written and read from a JSON configuration file. Additionally, store an auto-generated JSON schema file alongside the configuration file. To edit the configuration file after its creation, use the tool […] Read more...

Tags: , , , ,