private void genericsExample_Click(object sender, RoutedEventArgs e) { DataStore <string> store = new DataStore <string>(); store.Data = "string only"; //store.Data = 321;//this fails because once you initialized it with a certain data type, you can not change it again to different data type DataStore <double> market = new DataStore <double>(); market.Data = 321.45; KeyValuePair <int, string> kvp1 = new KeyValuePair <int, string>(); kvp1.Key = 100; kvp1.Value = "Hundred"; KeyValuePair <string, string> kvp2 = new KeyValuePair <string, string>(); kvp2.Key = "IT"; kvp2.Value = "Information Technology"; DataStore_v2 <int> test = new DataStore_v2 <int>(); test.data = 432432; GenericMethods <float> myGenericDataHolder = new GenericMethods <float>(); myGenericDataHolder.AddOrUpdate(0, 32.32112F); myGenericDataHolder.AddOrUpdate(0, 52.32112F); var vrDEfault = myGenericDataHolder.GetData(3212); GenericMethods <string> genericStringsHolder = new GenericMethods <string>(); genericStringsHolder.AddOrUpdate(0, 32.32112F.ToString()); genericStringsHolder.AddOrUpdate(0, 52.32112F.ToString()); var vrDEfaultString = genericStringsHolder.GetData(3212); genericMethodOverloadings <double> vrOverloads = new genericMethodOverloadings <double>(); vrOverloads.AddOrUpdate(32, 123.0); //number 1 vrOverloads.AddOrUpdate <int>(32, 123); //number 3 vrOverloads.AddOrUpdate(32.0, 123.0); //number 2 vrOverloads.AddOrUpdate(321); //number 4 CultureInfo.CurrentCulture = new CultureInfo("en-US"); Printer printClass = new MainWindow.Printer(); printClass.Print <int, double>("Example", "Student", 23, 87.32); Printer printClass2 = new MainWindow.Printer(); printClass.Print <double, int>("Example 2", "Student", 23.32, 87); }
private static void part5() { DataStore <string> store = new DataStore <string>(); store.Data = "such example"; Console.WriteLine(store.Data); //store.Data = 43242; you cant convert type to other types once assigned. compile time error DataStore <string> strStore = new DataStore <string>(); strStore.Data = "Hello World!"; //strStore.Data = 123; // compile-time error DataStore <int> intStore = new DataStore <int>(); intStore.Data = 100; //intStore.Data = "Hello World!"; // compile-time error KeyValuePair <int, string> kvp1 = new KeyValuePair <int, string>(); kvp1.Key = 100; kvp1.Value = "Hundred"; KeyValuePair <string, string> kvp2 = new KeyValuePair <string, string>(); kvp2.Key = "IT"; kvp2.Value = "Information Technology"; DataStore <long> ldData = new DataStore <long> { Data = 2312, DataArray = new long[] { 3213, 12312, 532432 } }; foreach (var item in ldData.DataArray) { Console.WriteLine(item); } ldData.AddOrUpdate(3, 321531532L); //this will call first AddOrUpdate ldData.AddOrUpdate(2321L, 12312L); //this will call second AddOrUpdate ldData.AddOrUpdate <double>(2321L, 3321.12); //this will call third AddOrUpdate var vrdata = ldData.GetData(3); Console.WriteLine(vrdata); Printer printer = new Printer(); printer.Print <int>(100); printer.Print(200); // type infer from the specified value printer.Print <string>("Hello"); printer.Print("World!"); // type infer from the specified value DataStore_v2 <ConsoleColor> mycolor = new DataStore_v2 <ConsoleColor>(); mycolor.Data = ConsoleColor.Red; // DataStore_v2<string> mycolor2 = new DataStore_v2<string>(); this will throw compile time error DataStore_v3 <string> dt3 = new DataStore_v3 <string>(); // DataStore_v3<int> dt4 = new DataStore_v3<int>(); this will throw compile time error DataStore_v3 <int[]> dt5 = new DataStore_v3 <int[]>(); DataStore_v3 <Dictionary <string, string> > dt6 = new DataStore_v3 <Dictionary <string, string> >(); TestStringEquality(); }