.NET Fiddle

CSharp · Console

/Program.cs

using System;

// Or maybe more meaningful than object
static SumType GetSomething(bool b) => b ? new SumType.IsInt(1) : new SumType.IsString("two");

static string Print(SumType input) => input switch {
	SumType.IsInt obj => $"is int: {obj.val}",
	SumType.IsString obj => $"is string: {obj.val}",
	_ => throw new NotSupportedException($"{input.GetType().Name} not implemented"), 
};


Console.WriteLine(Print(GetSomething(true)));
Console.WriteLine(Print(GetSomething(false)));
//Console.WriteLine(Print(false)); //Compile error


abstract record SumType {
	public record IsInt(int val) : SumType {};
	public record IsString(string val) : SumType {};
}
;