Gavin Draper's Blog

Father, Developer, Tech Geek, Extreme Sports Fanatic

@gavdraper | +gavdraper | github | StackOverflow Careers
 

C# Hidden Features

In the last few releases of C# a lot of new features have made there way in allowing
us to write less code. A lot of these just kind of snuck in to the language and didn't
get much press. I'm going to cover a couple of these new and old but less well known
features below.

Automatic Properties

Automatic properties allow you to create simple properties without having to create
the backing field.

For example standard properties look like this

private string foo;  
public string foo  
{
    get{return foo;}
    set{foo = value;}
}

With the addition of automatic properties the code above can be replaced with the
following

public string Foo{get;set;}

Not only is it less code but you then don't have to worry about accessing the field
when you wanted to access the property or accessing the property when you wanted the
field. Obviously this doesn't work if you want logic or manipulations to be run on
the field before it is set or retrieved, in that case you will have to declare the
field like you always did.

The ?? Operator

The ?? operator returns the first non null value. For example

string test = null;  
string test2 = "Hello world"  
Console.WriteLine(test??test2);

This will write “Hello World” to the console as test is null so it ignores it and
checks the next value. This can be daisy chained so you could have more than 2 values.

Object Initializers

Object initializers allow you to initialize the public properties of an object when
you create it e.g

public class Foo  
{
    public string Name{get;set;}
    public int Code{get;set;}
}

public void CreateFoo  
{
    var foo = new Foo();
    foo.Name = "MyFoo";
    foo.Code = 123;
}

Could become

public class Foo  
{
    public string Name{get;set;}
    public int Code{get;set;}
}

public void CreateFoo  
{
    var foo = new Foo(){Name="MyFoo",Code=123};
}

This simple example would be better achieved with a constructor, in reality this feature
works well on objects with multiple properties that cant be set by passing their values
into the constructor.

Watch this space for more in my next post...


 
comments powered by Disqus