Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# extension method for setting to new instance if null

I have the following extension methods to help me check and instantiate objects if they are null. The top two work just fine but they are not very useful.

    public static bool IsNull<T>(this T t)
    {
        return ReferenceEquals(t, null);
    }

    public static T NewIfNull<T>(this T t, Func<T> createNew)
    {
        if (t.IsNull<T>())
        {
            return createNew();
        }

        return t;
    }

    public static void Ensure<T>(this T t, Func<T> createNew)
    {
        t = t.NewIfNull<T>(createNew);
    }

Ultimately I would like to do something like

IList<string> foo;
...
foo.Ensure<IList<string>>(() => new List<string>());

The Ensure method however does not achieve the desired effect, which is setting foo to a instance of List<string> if it is null and basically set it to itself otherwise.

if you know now I can tweak the Ensure method to achieve this I would appreciate the help.

Thanks, Tom

like image 737
tleef Avatar asked Dec 08 '22 23:12

tleef


1 Answers

You need to distinguish between objects and variables. An object can never be null - the value of a variable can be. You're not trying to change something about an object (which would work) - you're trying to change the value of the caller's variable.

However, arguments are passed by value by default, which means that your extension method changes the parameter (the variable declared within the method), but this has no effect on the caller's variable. Normally you'd be able to change the parameter to ref to achieve pass-by-reference semantics, but extension methods can't have a ref or out first parameter.

As others have said, using the null-coalescing operator (??) is a better bet. Note that in this expression:

foo = foo ?? new List<string>();

the new list is not constructed unless foo is null. The right hand operand of ?? is not evaluated unless it needs to be.

like image 91
Jon Skeet Avatar answered Dec 11 '22 11:12

Jon Skeet