C#, Technology

How to convert from string to enum using C# and generics

In order to convert from a string to an enumeration in .NET you can use the method Enum.Parse as follows:

public enum SortExpressions
{
	FirstNameAsc,
	FirstNameDesc,
	LastNameAsc,
	LastNameDesc
}

var value = "lastnameasc";
var sortExpression = (SortExpressions) Enum.Parse(typeof(SortExpressions), value, true);

Since this operation is often needed we should make a ConvertToEnum method so we can reuse it. But instead of returning it as the typical object type, we should rather use generics to return it as the actual type: Read More »

C#, Technology

The C#using statement

In C#, when you want to make sure an object is disposed, then you might place you’re code within a try finally block as follows: Read More »

C#, Technology

The C# break statement

In C# you have the break statement which is most often seen in relation to terminating a switch statement. However it can also be used to terminate any loop statement as follows:

for (int i = 0; i < 10; i++)
{
	if (i == 5) break;
}

If you have nested loop- or switch statements, then the break statement will only terminate the closest one.

C#, Technology

How to avoid .NET naming collisions

In order to avoid .NET naming collisions you can use a namespace- or class alias. Read More »