Restart 2.0

Today I asked myself about restarting a program by code correctly. We all know the Process class and can kill the program directly but that’s not very nice. After a little search I stumbled over the Application.Restart() method. I tested it and it works fine ūüôā


Example for extension methods in C#

Today I found one real world example for using extension methods in C#.

Maybe you know this code snippet from your own code:
If(object != null)
MessageBox.Show(‚ÄěValue:‚Äú + object.ToString().Trim());

I think it looks way to complex, for this stupid task.
Now lets write a extension method which checks for us

=> if the object is null return an empty string

=> if the object is a whitespace string return an empty string too

=> if the object is a normal string, returns it but with a automated trim() to remove annoying white spaces.

How to achieve that:

1. Create a public static class called „StringOptimizer“ and then create the function ToStringIntelligent.

public static class StringOptimizer
public static string ToStringIntelligent(this object _object)
if (_object == null)
return string.Empty;
return _object.ToString().Trim();

See the extension in the parameter _object. You use this before parameter declaration, that’s all.

Now to call this function your just do this:
Console.WriteLine("Value:" + item.ToStringIntelligent());
Note, no validation have to happen here, cause we do it in the extension method already. Extra plus is, that this method actually works with all types that derives from object, so you can use it with your string variables as well. Of course, we could use string.IsNullOrEmpty() function from the .NET framework, but I like to do the validation in just one method, instead of writing validation code for all string objects manually.

Another example is here on codeproject

Software architecture in .NET Entity Framework app.

First I was a little bit confused how to structure my applications when I use .NET EF.

Here’s a good video where you’ll extract¬†your Entity Framework Model to a¬†separate¬†class library, which makes it easier to update clients or reuse your model in other projects like WPF Service, WEB, etc.

If you need further Business Logic you can follow this articles.


Make a .NET Assembly run as COM-Server

Because it’s the¬†second¬†project about COM i decided to write a short article about it. Com is great for extending old projects¬†with great and creative .NET code ūüėČ

How to achieve that:

1. Write a normal Class Library with Visual Studio

2. Mark your class with this attributes

public class UserHelper : IUserHelper

//Your Class Code


3. Implement an interface for your class with all your methods and properties that should be accessible via COM

public interface IUserHelper
void SomeMethod(int param1, string param2);

4. Build your library

5. Use regasm.exe to register the dll. I use

regasm.exe "PathToDll.dll" /codebase /regfile

5. Execute the generated .reg file for creating all needed registry keys

6. Test your COM Server via a little vbScript

obj.METHODNAME Param1,"Param2"
MsgBox "Well done!"

Notes: Never ever use the VS tools like ¬†„Make Assembly Com-Visible“ or „Register for COM interop“. It drives me crazy and is a waste of time at all. Keep in mind that this example is for platform target „Any CPU“. You should register it with reagsm.exe 32 bit wich is located in¬†C:\Windows\Microsoft.NET\Framework\v4.0.30319. 64 bit version of regasm.exe is here:¬†C:\Windows\Microsoft.NET\Framework64\v4.0.30319

Great article in .NET Pro (German):

Regasm.exe reference: