Handy C# Shortcuts

Rather than walking down an object model validating that each level is not null, like this:

if ((this.MyThing != null) && (this.MyThing.MySubThing != null))
{
    // do stuff
}

You can shortcut with a Null-conditional Operator:

if (this.MyThing?.MySubThing != null)
{
    // do stuff
}

Rather than explicitly checking and initializing a backing variable, like this:

public Thing MyThing
{
    get
    {
        if (_myThing == null)
        {
            _myThing = new Thing();
        }
        return _myThing;
    }
    set { _myThing = value; }
}

You can use a Null-coalescing Operator and make use of the fact that assigning a value to a variable also returns the value:

public Thing MyThing
{
    get { return _myThing ?? (_myThing = new Thing()); }
    set { _myThing = value; }
}

Rather than creating a read-only property without a setter, like this:

public Thing MyOtherThing
{
    get
    {
        return _myOtherThing;
    }
}

You can just use a Lambda Operator to return the backing variable:

public Thing MyOtherThing => _myOtherThing;

Nullable Types

Wrapping settings retrieved from the database in a Property and storing them in Private local variables is a good thing. If the variable is still set to its default value, we populate from the database. Good times.

Private Shared _someSetting As Integer = 0
Public Shared ReadOnly Property SomeSetting As Integer
    Get
        If _someSetting = 0 Then
            _someSetting = GetSomethingFromTheDatabase()
        End If
        Return _someSetting
    End Get
End Property

But what if the actual stored value is 0? It’ll reload from the database every time. Enter the Nullable Type: Nullable Value Types (Visual Basic) Nullable Types (C#)

Private Shared _someSetting As Nullable(Of Integer)
Public Shared ReadOnly Property SomeSetting As Integer
    Get
        If Not _someSetting.HasValue Then
            _someSetting = GetSomethingFromTheDatabase()
        End If
        Return _someSetting.Value
    End Get
End Property

It’s just like a normal Integer, plus it can also be Nothing. Now we populate the variable only if it actually has not been set, then we return the Value of the Nullable Type.

You can also shortcut the declaration like this:

Private Shared _someSetting As Integer?

As the name implies, you can use the Nullable Type with any Value Type: Value Types and Reference Types.

ByRef Parameters

Leaving out the “out” keyword, there’s four basic combinations available to us: some combination of a value or a reference type, passed ByVal or ByRef.

Passing a value type by value. (ValVal in the examples below.)

This one’s simple. You’re passing the actual value into the method. Any changes you make to the parameter variable inside the method are not visible in the caller.

Passing a value type by ref. (RefVal in the examples below.)

You’re passing a reference to the value. Any changes you make inside the method are visible in the caller.

Passing a reference type by value. (ValRef in the examples below.)

Here’s where it gets interesting. You’re passing a reference type. Any changes made to the members of the parameter variable are visible to the caller.

Passing a reference type by ref. (RefRef in the examples below.)

While I’m sure we could dream up a reason to do this, we’re not that cool. Just don’t. You’re passing a reference to a reference which 99.999% of the time provides no value.

Or as Microsoft says: DO NOT pass reference types by reference.

public class MyClass
{
    public int MyProperty { get; set; }
}

public class MyOtherClass
{
    public void ValVal(int myValue)
    {
        myValue = 6;
    }

    public void RefVal(ref int myValue)
    {
        myValue = 9;
    }

    public void ValRef(MyClass myObject)
    {
        myObject.MyProperty = 42;
    }

    public void RefRef(ref MyClass myObject)
    {
        myObject.MyProperty = 42;
    }
}

This is the simplified version. If anyone wants to have discussions about how technically the scope of a parameter variable is always just the method in which its defined because you’re actually passing pointers blah blah blah, let’s do lunch.

Or if you’re bored, go read Framework Design Guidelines. Good times.