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
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
refto achieve pass-by-reference semantics, but extension methods can’t have areforoutfirst parameter.As others have said, using the null-coalescing operator (??) is a better bet. Note that in this expression:
the new list is not constructed unless
foois null. The right hand operand of??is not evaluated unless it needs to be.