user22335954
user22335954

Reputation: 118

Dereferencing a nullable type

I've written a function that parses some arguments into a valid "thing". If the arguments can't be parsed into a valid thing, then this function will return a null.

I'm accessing the valid "thing" by casting it to the specific type, but I'm wondering if there is a concise way to "dereference" the thing instead of casting.

private Nullable<Thing> GetTheThing(string arg) 
{
    if (s == "") 
    {
        return new Thing(...);
    } 
    else 
    {
        return null;
    }
}

// ...snip...

var t = GetTheThing("asd")

if (t == null) 
{
    throw Exception("Thing could not be made from this arg");
}

// This function take a "Thing" as it's argument, NOT a NULLABLE.
// Casting just seems so brute force, I feel like t should already know what it is, and I've checked for null, so why can't I just do something like *t?
DoSomethingWithTheThing((Thing)t);

Upvotes: -3

Views: 134

Answers (2)

Dave Cousineau
Dave Cousineau

Reputation: 13198

Normally I would write it something like this:

var maybeThing = MaybeGetThing("asd");
if (maybeThing is not { } thing)
    throw Exception("Thing could not be made from this arg");

// do something with thing which will be non-nullable

MaybeGetThing (this could also be named GetThingOrDefault or GetThingOrNull):

Thing? MaybeGetThing(string arg) {
   if (arg == ...)
      return null;

   return new Thing(...);
}

To do this without "casting", and might be what you're looking for by saying 'dereference' you could do this, but I would not normally write it this way myself. Basically you can use .Value if you know that there should be a value. This works only with nullable structs and not nullable reference types. Saying nullablestruct.Value is kind of like saying "GetValueOrThrow", since you are asserting that the value should exist, and to throw an exception otherwise.

var maybeThing = MaybeGetThing("asd");
if (maybeThing is null)
    throw Exception("Thing could not be made from this arg");

DoSomethingWithTheThing(maybeThing.Value);

Some other kinds of methods that can be useful as well:

Thing GetThingOrThrow(string arg) {
   var maybeThing = MaybeGetThing(arg);
   if (maybeThing is not { } thing)
      throw ...;

   return thing;
}

bool TryGetThing(string arg, [MaybeNullWhen(false)] out Thing result) {
   result = MaybeGetThing(arg);
   return result is { };
}

bool CantGetThing(string arg, [MaybeNullWhen(true)] out Thing result) {
   result = MaybeGetThing(arg);
   return result is null;
}

The MaybeNullWhen attribute should provide IDE warnings when you use it the wrong way.

Example:

if (CantGetThing("arg", out var thing)) {
   // can't use thing, it is null
   return;
}
// can use thing since we returned in the case that it was null

Upvotes: 0

mason
mason

Reputation: 32702

You could accomplish this with out variables.

using System;
                    
public class Program
{
    public static void Main()
    {
        if (TryGetTheThing("thing", out Thing thing))
        {
            Console.WriteLine("I got a thing here");
        }
        else
        {
            Console.WriteLine("Thing will be null");
        }   
    }
    
    public static bool TryGetTheThing(string s, out Thing thing)
    {
        if (s == "thing")
        {
            thing = new Thing();
            return true;
        }
        else
        {
            thing = null;
            return false;
        }
    }
}

public class Thing
{
}

Working fiddle here.

But if you insist on making it nullable, that's doable too.

using System;
                    
public class Program
{
    public static void Main()
    {
        var t = GetTheThing("");
        
        if (t == null)
        {
            throw new Exception("Thing could not be made from this arg");
        }
        else
        {
            t.DoSomething();
        }
    }
    
    private static Thing? GetTheThing(string s)
    {
        if (s == "")
        {
            return new Thing();
        }
        else
        {
            return null;
        }
    }
}

public class Thing
{
    public void DoSomething () { Console.WriteLine("Hello, world!"); }
}

Working fiddle here.

Upvotes: -1

Related Questions