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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -