73

Java 1.8 is receiving the Optional<T> class, that allows us to explicitly say when a method may return a null value and "force" its consumer to verify if it is not null (isPresent()) before using it.

I see C# has Nullable<T>, that does something similar, but with basic types. It seems to be used for DB queries, to distinguish when a value exists and is 0 from when it doesn't exist and is null.

But it seems that C#'s Nullable<T>doesn't work for objects, only for basic types, while Java's Optional<T> only works for objects and not for basic types.

Is there a Nullable/Optional class in C#, that forces us to test if object exists before extracting and using it?

13
  • 7
    This is called the Maybe monad. C# doesn't have it.
    – SLaks
    Commented Apr 24, 2013 at 18:11
  • 7
    But reference types are already nullable, why is this needed? Commented Apr 24, 2013 at 18:11
  • 5
    @MikeChristensen I think OP is asking whether there is some interface/contract that forces users to check whether type is null before using it. Note that nullable DOES NOT fulfill this requirement. nullable simply boxes primitive types into objects so that they can be nullable. This seems to be different from the Optional class OP is describing
    – Porkbutts
    Commented Apr 24, 2013 at 18:14
  • 6
    For the record C# does not have "basic types" it has "value types" including user defined value types.
    – Stilgar
    Commented Apr 24, 2013 at 18:16
  • 3
    I used to think that C# was the end-all be-all of languages. I love the parameter-property-declarations of TypeScript, the language-integrated code-contracts of spec#, units of measure of f#, purity of Haskell (at least as an optional specification), undefined of javascript. I wish they would all have a love-child. Nothing from T-SQL is allowed in. Commented Apr 24, 2013 at 18:33

13 Answers 13

41

In my opinion, any Option implementation which exposes HasValue property is the defeat of the entire idea. The point of optional objects is that you can make unconditional calls to their contents without testing whether the content is there.

If you have to test whether the optional object contains a value, then you have done nothing new compared to common null tests.

Here is the article in which I am explaining optional objects in full detail: Custom Implementation of the Option/Maybe Type in C#

And here is the GitHub repository with code and examples: https://github.com/zoran-horvat/option

If you're reluctant to use a heavyweight Option solution, then you can easily build a lightweight one. You can make your own Option<T> type which implements IEnumerable<T> interface, so that you can leverage LINQ extension methods to turn calls optional. Here is the simplest possible implementation:

public class Option<T> : IEnumerable<T>
{
    private readonly T[] data;

    private Option(T[] data)
    {
        this.data = data;
    }

    public static Option<T> Create(T value)
    {
        return new Option<T>(new T[] { value });
    }

    public static Option<T> CreateEmpty()
    {
        return new Option<T>(new T[0]);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return ((IEnumerable<T>)this.data).GetEnumerator();
    }

    System.Collections.IEnumerator
        System.Collections.IEnumerable.GetEnumerator()
    {
        return this.data.GetEnumerator();
    }
}

Using this Option<T> type is done via LINQ:

Option<Car> optional = Option<Car>.Create(myCar);
string color = optional
  .Select(car => car.Color.Name)
  .DefaultIfEmpty("<no car>")
  .Single();  // you can call First(), too

You can find more about optional objects in these articles:

And you may refer to my video courses for more details on how to simplify control flow using Option type and other means: Making Your C# Code More Functional and Tactical Design Patterns in .NET: Control Flow

The first video course (Making Your C# Code More Functional) brings detailed introduction to railway-oriented programming, including the Either and Option types and how they can be used to manage optional objects and handle exceptional cases and errors.

15
  • 2
    Thanks. The point in using Optional is that, when some software error lets an object method or field be called on a pointer that's null, it will throw an Exception. This kind of error happens when somebody forgets to test the pointer for null before using it, and compiler lets the call be compiled without a test.
    – Hikari
    Commented Nov 25, 2016 at 19:31
  • 8
    My point is that you never test whether Optional contains anything. Just call the method and let Optional forward the call to the contained object if it contains anything. That is how you turn an optional call, call wrapped in an if-else, into an unconditional call on an optional object. Commented Nov 25, 2016 at 19:36
  • 2
    @urig This implementation from the answer is the basic one and it relies on LINQ for everything beyond constructing the Option. In another article I have implemented a complete Option type with full support for related operations. The article also contains GitHub repo link with the entire code. Here is the article: codinghelmet.com/?path=howto/… Commented Mar 23, 2018 at 22:51
  • 1
    @ZoranHorvat Since I didn't find the complete implementation (i.e. heavyweight version) of your option pattern on NuGet. I implemented it and I made it available. My code is mostly inspired by your course Making Your C# Code More Object-oriented. The code can be found here and the NuGet package can be downloaded here. Commented May 29, 2018 at 22:46
  • 1
    "If you have to test whether the optional object contains a value, then you have done nothing new compared to common null tests." Not necessarily. Seems most people look at Option in a vacuum. Any useful implementation would include Map, Filter, etc in which case the Some check is encapsulated in the type itself; how do you do that cleanly with a myriad of null checks scattered everywhere in your code and what do you do about it when it is null? Option covers that. Commented Feb 1, 2022 at 19:29
39

Not in the language, no, but you can make your own:

public struct Optional<T>
{
    public bool HasValue { get; private set; }
    private T value;
    public T Value
    {
        get
        {
            if (HasValue)
                return value;
            else
                throw new InvalidOperationException();
        }
    }

    public Optional(T value)
    {
        this.value = value;
        HasValue = true;
    }

    public static explicit operator T(Optional<T> optional)
    {
        return optional.Value;
    }
    public static implicit operator Optional<T>(T value)
    {
        return new Optional<T>(value);
    }

    public override bool Equals(object obj)
    {
        if (obj is Optional<T>)
            return this.Equals((Optional<T>)obj);
        else
            return false;
    }
    public bool Equals(Optional<T> other)
    {
        if (HasValue && other.HasValue)
            return object.Equals(value, other.value);
        else
            return HasValue == other.HasValue;
    }
}

Note that you won't be able to emulate certain behaviors of Nullable<T>, such as the ability to box a nullable value with no value to null, rather than a boxed nullable, as it has special compiler support for that (and a some other) behavior.

19
  • 17
    This wouldn't do any good. The only thing you're achieving with this is that you replace a NullReferenceException with an InvalidOperationException if the developer doesn't do the check. Apart from that you are changing the way he has to check for null. In the end, it doesn't add a lot value. It would be useful if you could force the check, but that's not possible.
    – Kenneth
    Commented Apr 24, 2013 at 18:17
  • 7
    As far as I know this is the case in Java too. Option in languages like Java and C# allows you to express the semantic possibility to return null from the method. There are languages where the compiler will force you to check but not these two. The problem with defining your own type is that only your code will use it and you kind of want to have the whole ecosystem using it.
    – Stilgar
    Commented Apr 24, 2013 at 18:21
  • 27
    @Kenneth The OP didn't ask for a way of avoiding null checks; he asked for a way to wrap the concept of an optional value into a type in C# (for classes as well as structs). This does that.
    – Servy
    Commented Apr 24, 2013 at 18:23
  • 3
    @Kenneth Well, one advantage is that you can unify the concept of nullability between structs and classes. Rather than having a different system for each type of object you can use this one system for any type of object. Now, would I use something like this in my programs, no, probably not. I don't see it being worthwhile, but I'm not going to say that the OP mustn't use such a class ever.
    – Servy
    Commented Apr 24, 2013 at 18:29
  • 3
    @Kenneth the other thing that this approach gives you is that you can then apply some reasoning about your program. If you use this approach everywhere then you can assume that nothing will ever be null (and even use AOP techniques to enforce that, see Fody.NullGuard) and it then becomes explicit when a return value or parameter is one that can be null, as it will be Optional<T>, and not just some object which you have no way of knowing if it is allowed to be null or not.
    – Sam Holder
    Commented Jan 11, 2014 at 15:31
13

There is better implementation of option type in C#. You can find this implemenation in Tactical design patterns in .NET by Zoran Horvat at pluralsight.com. It includes an explanation why and how to use it. The basic idea is to implement option class as implementation of IEnumerable<> interface.

public class Option<T> : IEnumerable<T>
{
    private readonly T[] data;

    private Option(T[] data)
    {
        this.data = data;
    }

    public static Option<T> Create(T element)
    {
        return new Option<T>(new[] { element });
    }

    public static Option<T> CreateEmpty()
    {
        return new Option<T>(new T[0]);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return ((IEnumerable<T>) this.data).GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}
1
  • 3
    He wrote his own answer here. Commented Sep 3, 2019 at 0:01
4

In the project "C# functional language extensions" https://github.com/louthy/language-ext exists the Option object of F# among others functional patters

1
  • as of the end of 2017 I would highly recommend adding this to your project. It includes a lot of other functional goodies but even if you don't understand them the option type is great. Commented Dec 29, 2017 at 22:03
4

Instead of writing your own class, you could use Microsoft.FSharp.Core.FSharpOption<T> from the FSharpCore.dll assembly. Unfortunately, the F# types are a bit clumsy when used in C#.

//Create
var none = FSharpOption<string>.None;
var some1 = FSharpOption<string>.Some("some1");
var some2 = new FSharpOption<string>("some2");

//Does it have value?
var isNone1 = FSharpOption<string>.get_IsNone(none);
var isNone2 = OptionModule.IsNone(none);
var isNone3 = FSharpOption<string>.GetTag(none) == FSharpOption<string>.Tags.None;

var isSome1 = FSharpOption<string>.get_IsSome(some1);
var isSome2 = OptionModule.IsSome(some1);
var isSome3 = FSharpOption<string>.GetTag(some2) == FSharpOption<string>.Tags.Some;

//Access value
var value1 = some1.Value; //NullReferenceException when None
var value2 = OptionModule.GetValue(some1); //ArgumentException when None
3

There's nothing built-in, but you can define your own. Note that an Option<T> implementation doesn't make sense without defining the map/bind operators.

public struct Option<T>
{
    private bool hasValue;
    private T value;

    public Option(T value)
    {
        if (value == null) throw new ArgumentNullException("value");
        this.hasValue = true;
        this.value = value;
    }

    public Option<TOut> Select<TOut>(Func<T, TOut> selector)
    {
        return this.hasValue ? new Option<TOut>(selector(this.value)) : new Option<TOut>();
    }

    public Option<TOut> SelectMany<TOut>(Func<T, Option<TOut>> bind)
    {
        return this.hasValue ? bind(this.value) : new Option<TOut>();
    }

    public bool HasValue
    {
        get { return this.hasValue; }
    }

    public T GetOr(T @default)
    {
        return this.hasValue ? this.value : @default;
    }
}
1
  • Not a fan of the Java-esq implementation but +1 for mentioning the operator requirements. Commented Jul 4, 2019 at 1:19
3

Perhaps this is closer to the F# Option type

public struct Option<T>
{
    private T value;
    private readonly bool hasValue;

    public bool IsSome => hasValue;

    public bool IsNone => !hasValue;

    public T Value
    {
        get
        {
            if (!hasValue) throw new NullReferenceException();
            return value;
        }
    }

    public static Option<T> None => new Option<T>();

    public static Option<T> Some(T value) => new Option<T>(value);

    private Option(T value)
    {
        this.value = value;
        hasValue = true;
    }

    public TResult Match<TResult>(Func<T, TResult> someFunc, Func<TResult> noneFunc) =>
        hasValue ? someFunc(value) : noneFunc();

    public override bool Equals(object obj)
    {
        if (obj is Option<T>)
        {
            var opt = (Option<T>)obj;
            return hasValue ? opt.IsSome && opt.Value.Equals(value) : opt.IsNone;
        }
        return false;
    }

    public override int GetHashCode() =>
        hasValue ? value.GetHashCode() : 0;
}
2

I decided to implement some kind of Optional<> Java class prototype some time ago using one of the last C# version.

Here it is:

public sealed class Optional<T>
{
    private static readonly Optional<T> EMPTY = new Optional<T>();
    private readonly T value;

    private Optional() => value = default;
    private Optional(T arg) => value = arg.RequireNonNull("Value should be presented");

    public static Optional<T> Empty() => EMPTY;
    public static Optional<T> Of(T arg) => new Optional<T>(arg);
    public static Optional<T> OfNullable(T arg) => arg != null ? Of(arg) : Empty();
    public static Optional<T> OfNullable(Func<T> outputArg) => outputArg != null ? Of(outputArg()) : Empty();

    public bool HasValue => value != null;

    public void ForValuePresented(Action<T> action) => action.RequireNonNull()(value);

    public IOption<T> Where(Predicate<T> predicate) => HasValue 
        ? predicate.RequireNonNull()(value) ? this : Empty() : this;

    public IOption<TOut> Select<TOut>(Func<T, TOut> select) => HasValue 
        ? Optional<TOut>.OfNullable(select.RequireNonNull()(value)) 
        : Optional<TOut>.Empty();

    public IOption<IOption<TOut>> SelectMany<TOut>(Func<T, IOption<TOut>> select) => HasValue 
        ? Optional<IOption<TOut>>.OfNullable(select.RequireNonNull()(value)) 
        : Optional<IOption<TOut>>.Empty();

    public T Get() => value;
    public T GetCustomized(Func<T, T> getCustomized) => getCustomized.RequireNonNull()(value);
    public U GetCustomized<U>(Func<T, U> getCustomized) => getCustomized.RequireNonNull()(value);

    public T OrElse(T other) => HasValue ? value : other;
    public T OrElseGet(Func<T> getOther) => HasValue ? value : getOther();
    public T OrElseThrow<E>(Func<E> exceptionSupplier) where E : Exception => HasValue ? value : throw exceptionSupplier();

    public static explicit operator T(Optional<T> optional) => OfNullable((T) optional).Get();
    public static implicit operator Optional<T>(T optional) => OfNullable(optional);

    public override bool Equals(object obj)
    {
        if (obj is Optional<T>) return true;
        if (!(obj is Optional<T>)) return false;
        return Equals(value, (obj as Optional<T>).value);
    }

    public override int GetHashCode() => base.GetHashCode();
    public override string ToString() => HasValue ? $"Optional has <{value}>" : $"Optional has no any value: <{value}>";
}
2

Use T? nullable reference instead of Option<T>

Since C#8 you should deprecate custom Option<T>-implementations. The null dilemma is now completely resolved.

T? is a complete substitution for Option<T>

C# has the following features for handling null:

  1. Null coalescing operator
  2. Null conditional operator
  3. Non nullable & nullable reference types (since C#8)
  4. Configurable compile errors/warnings

Keep in mind that

Option<Car> optional = Option<Car>.Create(myCar);
string color = optional
  .Select(car => car.Color.Name)
  .DefaultIfEmpty("<no car>")
  .Single();  // you can call First(), too

is the same as

string color = myCar?.Color.Name ?? "<no car>";

and additionally the string color is also a reference that can't be null.

13
  • 1
    I supported my answer with strong and valid arguments and I am highly interested in the reasons for the downvotes.
    – Felix Keil
    Commented Mar 13, 2020 at 9:14
  • 3
    Option<T> is not the same thing as a Nullable-reference in C# 8.0 though - sometimes "success" can be indicated by a null-value (e.g. returning a String value that contains an error message vs. returning null on success).
    – Dai
    Commented Apr 20, 2020 at 8:52
  • 1
    In your answer you talk about Option<T> vs T?, but that is kind of offtopic. You might be right and it might be a high quality comparison, but OP never asked about it. Apart from that: yea, people just downvoting without providing feedback is fruststrating. I hate it myself while being guilty of it too
    – Neuron
    Commented Aug 26, 2020 at 11:54
  • 1
    The problem with T? instead of Option<T> is that it assumes that null is not a valid return value, but a string can be null and an optional can have null ass a successful result so T? is more limited and not a full replacement. Commented Oct 26, 2021 at 13:23
  • 1
    @DavidMårtensson I don't understand your argument. T? explicitely states that null is a valid value. A string can't be null with nullable refs. Only string? can be null.
    – Felix Keil
    Commented Feb 1, 2022 at 13:19
1

If you don't like baking your own solutions, I would use Language Ext. It is available on nuget. I have recently started using this library, and the safety from null references is amazing! I am not a expert with this library, but it can do what you are asking for, and much more.

Here is a taste of what can be done:

using System;
using LanguageExt;
using static LanguageExt.Prelude;

public class Demo
{
    public static Option<int> ToEvenOrNone(int i) =>
        i % 2 == 0
            ? i.Apply(Optional)
            : None;

    public static void PrintToDebug(Option<int> value) => 
        value
            .Some(Console.WriteLine)
            .None(() => Console.WriteLine("Value is not Even!"));

    public static void Test()
    {
        for (int i = 0; i < 10; i++)
        {
            PrintToDebug(ToEvenOrNone(i));
        }
    }
}

Here is the output:

0
Value is not Even!
2
Value is not Even!
4
Value is not Even!
6
Value is not Even!
8
Value is not Even!
0
0

Is there a Nullable/Optional class in C#, that forces us to test if object exists before extracting and using it?

Nullables were created so that primitive types could be null. Their default value didn't have to be an actual value (Like int, without nullables it's default is 0, so is that a 0 means something 0 or a not set to anything 0?)

No there is nothing that you can do to force a programmer to check if an object is null. That's good though. Doing so would create an immense amount of overhead. If this was a language feature, how often would you force check? Would you require it when the variable is first assigned? What if the variable points to another object later? Would you force it to check before every method and property, and if it fails would you throw an exception? You get that now with a null reference exception. You would get very little benefit in forcing someone to do this beyond what you already have.

4
  • You wouldn't need to check - you would only provide a GetOr method which would require the user to provider a default value if there isn't one. You can easily define map\bind methods in C# to propagate missing values.
    – Lee
    Commented Apr 24, 2013 at 18:23
  • 2
    I totally agree with Kevin. On the other hand if code aesthetics are concerned you can always use an extention method to chack null value. static bool isPresent(this object instance) { return null != instance; }
    – cleftheris
    Commented Apr 24, 2013 at 18:25
  • null is essentially the default value, saying "Hey there's nothing here" It tells the programmer "I didn't set this to anything." If it has a default value, then you always have to know what that value is for any variable, property etc. It'll always be a guessing game of, "is this a real value or a default value." Null solves that, because it's always the default. Commented Apr 24, 2013 at 18:26
  • 6
    null isn't a default value - it's no value, and its use is unsafe. That is what Option<T> is for - to make the lack of a value explicit. With reference types in C# you can never express that a reference should always point to an object.
    – Lee
    Commented Apr 24, 2013 at 18:32
0

Learned a lot from Zoran Horvat's answer. Here is my code. optional can has a real value or an empty. On the consuming side, same code handle them all.

void Main()
{
    var myCar = new Car{ Color =  Color.Black, Make="Toyota"};

    Option<Car> optional = Option<Car>.Create(myCar);

    // optional is an Empty 50% of the time.
    if(new Random().NextDouble() > 0.5)
        optional = Option<Car>.CreateEmpty();



    string color = optional
    .Select(car => car.Color.Name)
    .DefaultIfEmpty("<no car>")
    .Single();
    Console.Write(color);
}

class Car {
    public Color Color { get; set; }
    public string Make { get; set;}
}

public class Option<T> : IEnumerable<T>
{
    private readonly T[] data;

    private Option(T[] data)
    {
        this.data = data;
    }

    public static Option<T> Create(T value)
    {
        return new Option<T>(new T[] { value });
    }

    public static Option<T> CreateEmpty()
    {
        return new Option<T>(new T[0]);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return ((IEnumerable<T>)this.data).GetEnumerator();
    }

    System.Collections.IEnumerator
        System.Collections.IEnumerable.GetEnumerator()
    {
        return this.data.GetEnumerator();
    }
}
1
  • 1
    The problem with this or any implementation inherited from IEnumerable (IE) is that Option contains at most one value where an IE has 0 or more. In addition to that just being generally confusing from an IE POV, it couples a non-standard API to Option that makes it confusing as well. For example, the entire IE interface aside from a few (Select, Where, and SelectMany). But more importantly, it lacks a None which will clutter your pipelines . If going the IE route is the best for your case, I'd suggest using composition over inheritance and hiding all that extra baggage. Commented Jul 5, 2021 at 20:49
0

https://github.com/mcintyre321/OneOf

I thought this oneOf class had a good re-creation of option type. It even includes a .switch/.match with pattern matching, and most importantly it works at runtime which is what you expect out of an Option pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.