How to check the return Status in C#

	public static class StatusExtensions {

		static StatusExtensions() {
			if ( Status.Good.IsBad() ) {
				throw new InvalidOperationException( "Someone blinked." );
			}

			if ( Status.Failure.IsGood() ) {
				throw new InvalidOperationException( "Someone blinked." );
			}

			if ( Status.Success.IsBad() ) {
				throw new InvalidOperationException( "Someone blinked." );
			}

			if ( !Status.Unknown.IsUnknown() ) {
				throw new InvalidOperationException( "Someone blinked." );
			}
		}

		[Pure]
		[MethodImpl( MethodImplOptions.AggressiveInlining )]
		public static Boolean Failed( this Status status ) => status <= Status.Failure;

		[Pure]
		[MethodImpl( MethodImplOptions.AggressiveInlining )]
		public static Boolean IsBad( this Status status ) => status.Failed();

		[Pure]
		[MethodImpl( MethodImplOptions.AggressiveInlining )]
		public static Boolean IsUnknown( this Status status ) => status == Status.Unknown || !status.IsBad() && !status.IsGood();

		[Pure]
		[MethodImpl( MethodImplOptions.AggressiveInlining )]
		public static Boolean Succeeded( this Status status ) => status >= Status.Success;

		[Pure]
		[MethodImpl( MethodImplOptions.AggressiveInlining )]
		public static Boolean IsGood( this Status status ) => status.Succeeded();

		[Pure]
		[NotNull]
		public static String Symbol( this Status status ) => status.GetDescription() ?? Symbols.Null;

		[Pure]
		[MethodImpl( MethodImplOptions.AggressiveInlining )]
		public static Status ToStatus( this Boolean status ) => status ? Status.Success : Status.Failure;

	}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s