Monday, June 11, 2012

What is the proper way to check for null values?


I love the null-coalescing operator because it makes it easy to assign a default value for nullable types.




int y = x ?? -1;



That's great, except if I need to do something simple with x . For instance, if I want to check Session , then I usually end up having to write something more verbose.



I wish I could do this:




string y = Session["key"].ToString() ?? "none";



But you can't because the .ToString() gets called before the null check so it fails if Session["key"] is null. I end up doing this:




string y = Session["key"] == null ? "none" : Session["key"].ToString();



It works and is better, in my opinion, than the three-line alternative:




string y = "none";
if (Session["key"] != null)
y = Session["key"].ToString();



Even though that works I am still curious if there is a better way. It seems no matter what I always have to reference Session["key"] twice; once for the check, and again for the assignment. Any ideas?


Source: Tips4all

9 comments:

  1. What about

    string y = (Session["key"] ?? "none").ToString();

    ReplyDelete
  2. If you're frequently doing this specifically with ToString() then you could write an extension method:

    public static string NullPreservingToString(this object input)
    {
    return input == null ? null : input.ToString();
    }

    ...

    string y = Session["key"].NullPreservingToString() ?? "none";


    Or a method taking a default, of course:

    public static string ToStringOrDefault(this object input, string defaultValue)
    {
    return input == null ? defaultValue : input.ToString();
    }

    ...

    string y = Session["key"].ToStringOrDefault("none");

    ReplyDelete
  3. You could also use as, which yields null if the conversion fails:

    Session["key"] as string ?? "none"


    This would return "none" even if someone stuffed an int in Session["key"].

    ReplyDelete
  4. If it will always be a string, you can cast:

    string y = (string)Session["key"] ?? "none";


    This has the advantage of complaining instead of hiding the mistake if someone stuffs an int or something in Session["key"]. ;)

    ReplyDelete
  5. All of the suggested solutions are good, and answer the question; so this is just to extend on it slightly. Currently the majority of answers only deal with null validation and string types. You could extend the StateBag object to include a generic GetValueOrDefault method, similar to the answer posted by Jon Skeet.

    A simple generic extension method that accepts a string as a key, and then type checks the session object. If the object is null or not the same type, the default is returned, otherwise the session value is returned strongly typed.

    Something like this

    /// <summary>
    /// Gets a value from the current session, if the type is correct and present
    /// </summary>
    /// <param name="key">The session key</param>
    /// <param name="defaultValue">The default value</param>
    /// <returns>Returns a strongly typed session object, or default value</returns>
    public static T GetValueOrDefault<T>(this HttpSessionState source, string key, T defaultValue)
    {
    // check if the session object exists, and is of the correct type
    object value = source[key]
    if (value == null || !(value is T))
    {
    return defaultValue;
    }

    // return the session object
    return (T)value;
    }

    ReplyDelete
  6. My preference, for a one off, would be to use a safe cast to string in case the object stored with the key isn't one. Using ToString() may not have the results you want.

    var y = Session["key"] as string ?? "none";


    As @Jon Skeet says, if you find yourself doing this a lot an extension method or, better, yet maybe an extension method in conjunction with a strongly typed SessionWrapper class. Even without the extension method, the strongly typed wrapper might be a good idea.

    public class SessionWrapper
    {
    private HttpSessionBase Session { get; set; }

    public SessionWrapper( HttpSessionBase session )
    {
    Session = session;
    }

    public SessionWrapper() : this( HttpContext.Current.Session ) { }

    public string Key
    {
    get { return Session["key"] as string ?? "none";
    }

    public int MaxAllowed
    {
    get { return Session["maxAllowed"] as int? ?? 10 }
    }
    }


    Used as

    var session = new SessionWrapper(Session);

    string key = session.Key;
    int maxAllowed = session.maxAllowed;

    ReplyDelete
  7. We use a method called NullOr.

    Usage

    // Call ToString() if it’s not null, otherwise return null
    var str = myObj.NullOr(obj => obj.ToString());

    // Supply default value for when it’s null
    var str = myObj.NullOr(obj => obj.ToString()) ?? "none";

    // Works with nullable return values, too —
    // this is properly typed as “int?” (nullable int)
    // even if “Count” is just int
    var count = myCollection.NullOr(coll => coll.Count);

    // Works with nullable input types, too
    int? unsure = 47;
    var sure = unsure.NullOr(i => i.ToString());


    Source

    /// <summary>Provides a function delegate that accepts only value types as return types.</summary>
    /// <remarks>This type was introduced to make <see cref="ObjectExtensions.NullOr{TInput,TResult}(TInput,FuncStruct{TInput,TResult})"/>
    /// work without clashing with <see cref="ObjectExtensions.NullOr{TInput,TResult}(TInput,FuncClass{TInput,TResult})"/>.</remarks>
    public delegate TResult FuncStruct<in TInput, TResult>(TInput input) where TResult : struct;

    /// <summary>Provides a function delegate that accepts only reference types as return types.</summary>
    /// <remarks>This type was introduced to make <see cref="ObjectExtensions.NullOr{TInput,TResult}(TInput,FuncClass{TInput,TResult})"/>
    /// work without clashing with <see cref="ObjectExtensions.NullOr{TInput,TResult}(TInput,FuncStruct{TInput,TResult})"/>.</remarks>
    public delegate TResult FuncClass<in TInput, TResult>(TInput input) where TResult : class;

    /// <summary>Provides extension methods that apply to all types.</summary>
    public static class ObjectExtensions
    {
    /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
    /// <typeparam name="TInput">Type of the input value.</typeparam>
    /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
    /// <param name="input">Input value to check for null.</param>
    /// <param name="lambda">Function to apply the input value to if it is not null.</param>
    public static TResult NullOr<TInput, TResult>(this TInput input, FuncClass<TInput, TResult> lambda) where TResult : class
    {
    return input == null ? null : lambda(input);
    }

    /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
    /// <typeparam name="TInput">Type of the input value.</typeparam>
    /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
    /// <param name="input">Input value to check for null.</param>
    /// <param name="lambda">Function to apply the input value to if it is not null.</param>
    public static TResult? NullOr<TInput, TResult>(this TInput input, Func<TInput, TResult?> lambda) where TResult : struct
    {
    return input == null ? null : lambda(input);
    }

    /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
    /// <typeparam name="TInput">Type of the input value.</typeparam>
    /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
    /// <param name="input">Input value to check for null.</param>
    /// <param name="lambda">Function to apply the input value to if it is not null.</param>
    public static TResult? NullOr<TInput, TResult>(this TInput input, FuncStruct<TInput, TResult> lambda) where TResult : struct
    {
    return input == null ? null : lambda(input).Nullable();
    }
    }

    ReplyDelete
  8. create an auxiliary function

    public static String GetValue( string key, string default )
    {
    if ( Session[ key ] == null ) { return default; }
    return Session[ key ].toString();
    }


    string y = GetValue( 'key', 'none' );

    ReplyDelete
  9. Skeet's answer is the best - in particularly I think his ToStringOrNull() is quite elegant and suits your need best. I wanted to add one more option to the list of extension methods:

    Return original object or default string value for null:

    // Method:
    public static object OrNullAsString(this object input, string defaultValue)
    {
    if (defaultValue == null)
    throw new ArgumentNullException("defaultValue");
    return input == null ? defaultValue : input;
    }

    // Example:
    var y = Session["key"].OrNullAsString("defaultValue");


    Use var for the returned value as it will come back as the original input's type, only as the default string when null

    ReplyDelete