C# codes that improve productivity - Part 1

Below I have listed 5 C# shorthands that you can use to make your code tighter and less wordy. No doubt you know one or more already — but do you currently use all five of them ?

1. The ? conditional operator

It allows you to compress a common if-then-else pattern into a single assignment.
int x = 10;
int y = 20;
int max;

if (x > y)
max = x;
else
max = y;

Using the (Question) ? Positive Answer : Negative Answer patterns the above can be rewritten as:
int x = 10;
int y = 20;
int max = (x > y) ? x : y;

2. Null-Coalesce operator (??)

How often do you test for null values in your code? Often? Then the null-coalesce operator (??) comes in handy.

To see how it works consider the following code example:
object cache = null;
object d = new object();
object e;

if (c != null)
e = c; // c is an object
else
e = d;

t is obvious that we can rewrite this using the single ? operator :
object cache = null;
object d = new object();
object e = (c != null) ? c : d;

3. Object Initializers

After you create a new object you often have to assign one or more of its properties. With the introduction of C# 3.0 it is now possible to use object initializers to both improve the readability of this, and to shorten your code.
Customer c = new Customer();
c.Name = "James";
c.Address = "204 Lime Street";

can be re-written as:
Customer c = new Customer { Name="James", Address = "204 Lime Street" };

4. The using statement

Often you will need to allocate a system resource such as a font, file handle, network resource etc. Each time you need such a resource there are three critical steps to go through: You acquire the resource, you use it, and then you dispose of it.

If you forget to properly dispose of it you will have created a memory or resource leak. This is best illustrated through the following patterns:
// 1. Allocation of the object
Font font1 = new Font("Arial", 10.0f);
try
{
// 2. The bit where we use the resource
}
finally
{
// 3. Disposal of the object
if (font1 != null)
((IDisposable)font1).Dispose();
}

The using statement allows us to compress this down to:
// Allocate the resource
using (Font font1 = new Font("Arial", 10.0f))
{
// The bit where we use the resource
}
// Disposal is automatic

The using statement is intended to be used with objects that implement the "IDisposable" interface which in practice is all .NET objects that allocate and manage resources.

5. Aliases for long winded namespaces and types

The names of C# identifiers can become quite long. If you are doing Microsoft Office automation in C# you might want to do something simple like open MS Word and change a document. You can use the "using" statement to create an alias for either a class or a namespace.
using Word = Microsoft.Office.Interop.Word;
...
Word.Application = new Word.Application() { Visible = True; }

Thanks : dijksterhuis

Read more tips at C# codes that improve productivity - Part 2

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

Leave a Reply