c# - a method that takes Dictionary with different data types -
the following code goes class called formula , generates numbers , labels formula. these come in 2 separate dictionarys because numbers double data type , labels string.
formula formula = new formula(formula_type, primary_output, fat_values); dictionary<string, double> numbers = formula.generatenumbers(); dictionary<string, string> labels = formula.generatelabels();
i trying create method can fed either of these dictionarys stuck on put in data type in method signature (see ??? in code below).
private static void displaydata(string text, dictionary<string, ???> dict) { string words = "the " + text + " dictionary contains following:"; console.out.writeline(words); foreach (var pair in dict) { console.out.writeline(pair.key + "=>" + pair.value); } console.out.writeline(""); }
is there simple way accomplish this? if solution complex, alternative approach preferable because simplicity important.
use generic method
private static void displaydata<t>(string text, dictionary<string, t> dict) { string words = "the " + text + " dictionary contains following:"; console.out.writeline(words); foreach (var pair in dict) { console.out.writeline(pair.key + "=>" + pair.value.tostring()); } console.out.writeline(""); }
to call:
displaydata<string>("text",labels);
Comments
Post a Comment