C# codes that improve productivity - Part 2

Dec 14, 2009 Posted by Lara Kannan
My early post, I explained five c# shorthands code tips. Again, I have listed another 5 C# shorthands that you can use to make your code too short.

Read my previous post at C# codes that improve productivity - Part 1

6. Nullable objects

A variable needs to have a value, it cannot be null. Sometimes it would be handy it was possible to assign "null" (eg. undefined) to a variable.

.NET 2.0 introduced the Nullable generic that makes this possible. The following two lines produce exactly the same object:
Nullable x = null;
int? x = null;

By putting a ? following a variable definition the compiler will wrap a Nullable generic around the type.

7. Automatic Properties

C# 3.0 introduced automatic properties. A property typically consists of a private variable which is exposed to the outside world through getters and setters (get;set;).

The following is common example of this:
public class Person
private string _firstName;
public string FirstName
get { return _firstName; }
set { _firstName = value; }

From C# 3.0 on we can reduce the above code to:
public class Person
public string Firstname { get; set; }

The C# compiler will automatically generate a backing variable and the correct get and set properties. Why is this useful? After all you could have just made a string variable in the class public instead.

By defining it as a property allows you to add the actual validation logic to your class at a later stage. The in-memory signature of the class won’t change which means that any external libraries compiled against your code will not have to be recompiled.

8. Type inference

In typical C# you will always carefully spell out your definitions:
string MyString = "Hello World";

From the right side of the declaration it is obvious that only one type (string) will ever match this definition. So instead of us doing the work, why not let the compiler figure this out?
var MyString = "Hello World";

The above definition will also create a string variable named "MyString". It is important to note that C# is still strongly typed.There is no performance impact results from using type inference.

The compiler does all the work figuring out the data type at compile time. Of course this feature created two opposite camps, one that thinks var should be liberally applied, and another one that abhors the whole idea. The middle ground seems to be that var should be used there were its use is obvious.
var SeniorStaff = Employees.Where(s => s.Age > 35);
foreach(var Member in SeniorStaff)
Console.WriteLine("Name: {0} Age: {1}",Member.Name,Member.Age);

For example what type would SeniorStaff be ?

9. Lambda Expressions

C# 2.0 introduced anonymous methods, which are methods defined inside a method. Incredibly powerful and a nice way to put all kinds of evaluation logic inside your code they had the drawback that they could be quite hard to read.
Func mySeniorStaffFilter = delegate(int a) { return a > 35; }; 

The above method takes an integer as a parameter, and returns a boolean. It checks if the staff member passed to it is older than 35. If so, it returns true.

Lamba expressions make things a little easier to read, while being functionally exactly the same:
Func mySeniorStaffFilter = a => a > 35; 

Even better, you can define them anywhere a delegate would have fitted:
var SeniorStaff = Employees.Where(s => s.Age > 35); 

10. string.IsNullOrEmpty

Not really a language feature, but a useful little library function in its own right. How often do you need to test if a string is empty? Or null? The string.IsNullOrEmpty method returns true if this is the case.
if (String.IsNullOrEmpty(s) == true)
return "is null or empty";
return String.Format("(\"{0}\") is not null or empty", s);

Thanks : dijksterhuis

Hope this will help you. A 'Thank You' would be nice!


Post a Comment