Underutilised in C#

I find that there are many underutilised features of C#. The following is short list of some of the features I would like to see being used more often.

The ?? operator

When using lazy loading or singleton constructs we often write code similar to this:

    public Address Address
    {
      get

      {
        if (address == null)

        {
          address = loadAddress();
        }
        return address;
      }
    }

This MSDN page explains that the ?? operator returns the left-hand operand if it is not null, or else it returns the right operand. Using ?? we could get the same functionality, with fewer lines of code:

    public Address Address
    {
      get
      {
        address = address ?? loadAddress();
        return address;
      }
    }

Is Null or Empty

Are you getting tired of writing code like this?

if (name == null || name.Equals(string.Empty))
{
// Do something
}

You really should not be using that, replace it with this:

if (String.IsNullOrEmpty(name))
{
// Do something
}

Using this will often allow us to use the ternary operator e.g.

    public string Name
    {
      get
      {
        return (String.IsNullOrEmpty(name) ? UNSPECIFIED : name);
      }
    }

List(T).AsReadOnly

To prevent modifications to a list, return the list wrapped in a read only wrapper. This aids in encapsulation and helps to prevent unwanted changes to collections. This MSDN page provides a good example.

Wanted: A Good Ruby on Rails 2.0 Tutorial

There have been many enhancements to the Rails framework. These changes have effected the scaffolding, exception handling , collection fixtures etc. For details about these and other changes I recommend this page.

If you are looking for a Rails 2.0 tutorial, that actually works, then you should visit Fairleads. This tutorial works perfectly. It will help you get to grips with the basics of the Model-View-Controller design of Rails.

If you are using Windows use Instant Rails to get up and running quickly! Instant Rails is a complete package containing Ruby, Rails, Apache, and MySQL. It does not need to be installed. It runs from within a folder of your choice. It will not modify your system. It is not the best looking system, but it does the job well.

 

 

Obviously you can use Instant Rails and still follow the Fairleads tutorial. Where Fairleads uses the ‘work’ directory you should use the ‘rails_apps’ folder. You should have your Rails ‘exchange’ application up and running in no time…