mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-26 03:58:45 +08:00
51 lines
954 B
C#
51 lines
954 B
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace mRemoteNG.Tools
|
|
{
|
|
public class Maybe<T> : IEnumerable<T>
|
|
{
|
|
private readonly T[] _maybe;
|
|
|
|
public Maybe()
|
|
{
|
|
_maybe = new T[0];
|
|
}
|
|
|
|
public Maybe(T value)
|
|
{
|
|
_maybe = value != null
|
|
? new[] {value}
|
|
: new T[0];
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
|
|
public IEnumerator<T> GetEnumerator()
|
|
{
|
|
return ((IEnumerable<T>)_maybe).GetEnumerator();
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return _maybe.Any() ? _maybe.First().ToString() : "";
|
|
}
|
|
|
|
public static implicit operator Maybe<T>(T value)
|
|
{
|
|
return new Maybe<T>(value);
|
|
}
|
|
|
|
public static Maybe<TOut> FromNullable<TOut>(TOut? value) where TOut : struct
|
|
{
|
|
return value.HasValue
|
|
? new Maybe<TOut>(value.Value)
|
|
: new Maybe<TOut>();
|
|
}
|
|
}
|
|
}
|