C# Tips, Tricks and Handy Hints

20 Apr

Part of the reason I started a Development blog in the first place was to create a common place to record all things dev that I didn’t want to forget.  Lots of these points fall into a ‘handy to know little tip that I might use more often than not but not worth writing a whole blog about’ sort of category!!  So I’ve created a dedicated blog post just for collecting all these small C# tips and tricks together in one place starting with Ordering Custom Collections.

Feel free to pass on your own tips and tricks and I’ll add them to the Post

TIP 1 : Ordering a Generic List Collection using Lambda expressions

Assume that I have a collection of List<Customer> Objects called Customers and I want to sort by Customer.Surname in alphabetical order

The easiest way is with this simple one line of code:

return Customers.OrderBy(x => x.Surname).ToList();

TIP 2 : Using Linq to Split a string and convert to an array of a different type using Lambda expressions

We all know you can use the Split function to split a string by a specified character and populate a string array.  However what if you want to populate an array of integers for example.  You might think we have to loop through each element in the string array to cast the element as an integer and populate into a new integer array.  Well with Linq you can do all this with one line of code:

string _numbers = “2126,2128,234,2”;

Int32[] iNumbers = Array.ConvertAll(_numbers.Split(‘,’), x => Convert.ToInt32(x));

TIP 3 : Distinct on a Linq to Entities Statement

Sticking with the Linq theme, we all know about performing a ‘SELECT DISTINCT ****’ style statement in SQL but how can we perform a similar Distinct when writing a Linq statement. Heres how:

(from cust in entities.Customers select cust).Distinct();

Simple but handy to know

TIP 4 : Getting the Assembly Version of the Current Project

I was working on a windows form project the other day and had a requirement to display the Version of the Assembly on the form.  To grab the version from the AssemblyInfo realting to the current project you can do the following:

string appName = System.Reflection.Assembly.GetAssembly(this.GetType()).Location;

System.Reflection.AssemblyName assemblyName = System.Reflection.AssemblyName.GetAssemblyName(appName);

string version = assemblyName.Version.ToString();

TIP 5 : Inserting a Quote into a string

Very quick this one, but one of those things that always gets passed over the table ‘How do you do that again?’

Simple preceed the Quote with the escape character ‘\’

For Example to output “This is a string surrounded by quotes” use

string sentence = “\”This is a string surrounded by quotes\””;

Leave a comment

Posted by on April 20, 2011 in ASP.NET, C#, Linq


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: