What you are trying to do is not type safe. For example, let's say you have a dictionary like this:
var foo = new Dictionary<string, (string, int, bool)>(); var x = foo["key1"];
One idea would be to develop a container class that can contain one or a string , int or bool .
public class StringIntBool { private bool _isSet; private bool _isString; public bool IsString { get { return _isString; } }
This is pretty ugly and doesn't really provide many benefits.
Alternatively, you could save all three values ββas object s, and then use a technique / library like Functional C # to perform pattern matching, as many functional languages ββcan.
object x = "str"; int res = x.Match() .With<string>(s => s == "str" ? 10 : 20) .With<int>(i => i) .With<bool>(b => b ? 50 : 60) .Return<int>();
This programming scheme is actually quite common in a specific functional language. For example, in SML, you can define a data type and then map it to a template as needed.
(* StringIntBool *) datatype sib = SibString of string | SibInt of int | SibBool of bool val x = (* some instance of sib *) val y = case x of SibString s => if s = "hello" then 50 else -50 | SibInt i => i | SibBool b => if b then 10 else 20
source share