static void Main(string[] args) { // Not generic helper testing NotGenericListHelper NotGeneric = new NotGenericListHelper(); List <string> strings = new List <string>() { "str1", "str2", "str3" }; List <int> ints = new List <int>() { 5, 22, -18 }; List <bool> bools = new List <bool> { true, false, true }; NotGeneric.GoThroughStrings(strings); NotGeneric.GetInfoForStrings(strings); NotGeneric.GoThroughInts(ints); NotGeneric.GetInfoForInts(ints); //Generic methods in generic class GenericListHelper <string> genericHelper1 = new GenericListHelper <string>(); GenericListHelper <int> genericHelper2 = new GenericListHelper <int>(); GenericListHelper <bool> genericHelper3 = new GenericListHelper <bool>(); GenericListHelper <string> .GoThrough(strings); GenericListHelper <string> .GetInfo(strings); GenericListHelper <int> .GoThrough(ints); GenericListHelper <int> .GetInfo(ints); GenericListHelper <bool> .GoThrough(bools); GenericListHelper <bool> .GetInfo(bools); }
static void Main(string[] args) { #region Generics with primitive types List <string> strings = new List <string>() { "str1", "str2", "str3" }; List <int> ints = new List <int>() { 5, 22, -18 }; List <bool> bools = new List <bool> { true, false, true }; // Not generic methods Console.WriteLine("NOT GENERIC --------------"); NotGenericListHelper.GoThrougStrings(strings); NotGenericListHelper.GetInfoForStrings(strings); NotGenericListHelper.GoThrougInts(ints); NotGenericListHelper.GetInfoForInts(ints); NotGenericListHelper.GoThrougBools(bools); NotGenericListHelper.GetInfoForBools(bools); // THIS IS AN EXAMPLE FOR GENERIC METHODS Console.WriteLine("GENERIC METHODS --------------"); // GenericListHelper.GoThrough<string>(strings); // OG type of writing generic methods // Even though the above example is a correct implementation, it has a simple version. The one bellow GenericListHelper.GoThrough(strings); GenericListHelper.GetInfo(strings); GenericListHelper.GoThrough(ints); GenericListHelper.GetInfo(ints); GenericListHelper.GoThrough(bools); GenericListHelper.GetInfo(bools); List <char> chars = new List <char>() { 'c', 'b', '0' }; GenericListHelper.GoThrough(chars); GenericListHelper.GetInfo(chars); // Generic class GenericListHelperClass <string> genericHelper1 = new GenericListHelperClass <string>(); GenericListHelperClass <int> genericHelper2 = new GenericListHelperClass <int>(); GenericListHelperClass <bool> genericHelper3 = new GenericListHelperClass <bool>(); Console.WriteLine("GENERIC CLASS --------------"); genericHelper1.GoThrough(strings); genericHelper1.GetInfo(strings); genericHelper2.GoThrough(ints); genericHelper2.GetInfo(ints); genericHelper3.GoThrough(bools); genericHelper3.GetInfo(bools); Console.ReadLine(); #endregion #region Generics with complex types // When we call the insert method on OrderDb the items will be stored in a list of orders // When we call the insert method on ProductDb the items will be stored in a list of products Console.Clear(); OrderDb.Insert(new Order() { Id = 1, Address = "Bob street 29", Receiver = "Bob" }); OrderDb.Insert(new Order() { Id = 2, Address = "Jill street 31", Receiver = "Jill" }); OrderDb.Insert(new Order() { Id = 3, Address = "Greg street 11", Receiver = "Greg" }); ProductDb.Insert(new Product() { Id = 1, Description = "For gaming", Title = "Mouse" }); ProductDb.Insert(new Product() { Id = 2, Description = "Mechanical", Title = "Keyboard" }); ProductDb.Insert(new Product() { Id = 3, Description = "64GB", Title = "USB" }); Console.WriteLine("-----------------------------"); Console.WriteLine("Orders:"); OrderDb.PrintAll(); Console.WriteLine("Products:"); ProductDb.PrintAll(); Console.WriteLine("------ Get by id 1 from Orders -----"); Console.WriteLine(OrderDb.GetById(1).GetInfo()); Console.WriteLine("------ Get by id 1 from Products -----"); Console.WriteLine(ProductDb.GetById(1).GetInfo()); Console.WriteLine("------ Removing id 1 from Orders -----"); OrderDb.DeleteById(1); Console.WriteLine("------ Removing id 1 from Products -----"); ProductDb.DeleteById(1); Console.WriteLine("-----------------------------"); Console.WriteLine("Orders:"); OrderDb.PrintAll(); Console.WriteLine("Products:"); ProductDb.PrintAll(); Console.ReadLine(); #endregion }
static void Main(string[] args) { #region Not generic methods List <string> names = new List <string> { "Martin", "Petre", "Teodora", "Eva" }; List <int> numbers = new List <int> { 1, 2, 3, 4, 5, 56, 6 }; NotGenericListHelper listHelper = new NotGenericListHelper(); listHelper.GoThroughStrings(names); listHelper.GetInfoForStrings(names); Console.WriteLine("======================"); listHelper.GoThroughIntegers(numbers); listHelper.GetInfoForIntegers(numbers); #endregion Console.WriteLine("========================== Using Generics ============================"); #region Generic Methods List <bool> checkList = new List <bool> { true, true, false, false, false }; GenericListHelper genericListHelper = new GenericListHelper(); genericListHelper.GoThrough(checkList); genericListHelper.GoThrough(names); genericListHelper.GoThrough(numbers); genericListHelper.GetInfo(checkList); genericListHelper.GetInfo(names); genericListHelper.GetInfo(numbers); #endregion Console.WriteLine("========================== Using Generic classes and methods ============================"); #region Generic classes and methods GenericClassListHelper <string> stringHelper = new GenericClassListHelper <string>(); GenericClassListHelper <int> intHelper = new GenericClassListHelper <int>(); stringHelper.GoThrough(names); stringHelper.GenericProp = "Martin"; intHelper.GoThrough(numbers); intHelper.GenericProp = 2; GenericClassListHelper <bool> .GetInfo(checkList); GenericClassListHelper <int> .GetInfo(numbers); GenericClassListHelper <string> .GetInfo(names); #endregion Console.ReadLine(); }
static void Main(string[] args) { #region Not Generic Helpers NotGenericListHelper NotGeneric = new NotGenericListHelper(); List <string> strings = new List <string>() { "str1", "str2", "str3" }; List <int> ints = new List <int>() { 5, 22, -18 }; List <bool> bools = new List <bool> { true, false, true }; NotGeneric.GoThroughStrings(strings); NotGeneric.GetInfoForStrings(strings); NotGeneric.GoThroughInts(ints); NotGeneric.GetInfoForInts(ints); #endregion #region Generic Helpers // For non static methods ( Uncomment only if you remove static from the methods ) //GenericListHelper<string> genericHelper1 = new GenericListHelper<string>(); //GenericListHelper<int> genericHelper2 = new GenericListHelper<int>(); //GenericListHelper<bool> genericHelper3 = new GenericListHelper<bool>(); //genericHelper1.GoThrough(strings); //genericHelper1.GetInfo(strings); //genericHelper2.GoThrough(ints); //genericHelper2.GetInfo(ints); //genericHelper3.GoThrough(bools); //genericHelper3.GetInfo(bools); // For static methods GenericListHelper <string> .GoThrough(strings); GenericListHelper <string> .GetInfo(strings); GenericListHelper <int> .GoThrough(ints); GenericListHelper <int> .GetInfo(ints); GenericListHelper <bool> .GoThrough(bools); GenericListHelper <bool> .GetInfo(bools); #endregion #region Generic Classes OrderDb.Insert(new Order() { Id = 1, Address = "Bob street 29", Receiver = "Bob" }); OrderDb.Insert(new Order() { Id = 2, Address = "Jill street 31", Receiver = "Jill" }); OrderDb.Insert(new Order() { Id = 3, Address = "Greg street 11", Receiver = "Greg" }); ProductDb.Insert(new Product() { Id = 1, Description = "For gaming", Title = "Mouse" }); ProductDb.Insert(new Product() { Id = 2, Description = "Mechanical", Title = "Keyboard" }); ProductDb.Insert(new Product() { Id = 3, Description = "64GB", Title = "USB" }); Console.WriteLine("Orders:"); OrderDb.PrintAll(); Console.WriteLine("Produts:"); ProductDb.PrintAll(); Console.WriteLine("-------Get by id 1 from Order and Product-------"); Console.WriteLine(OrderDb.GetById(1).GetInfo()); Console.WriteLine(ProductDb.GetById(1).GetInfo()); Console.WriteLine("-------Get by index 1 from Order and Product-------"); Console.WriteLine(OrderDb.GetByIndex(1).GetInfo()); Console.WriteLine(ProductDb.GetByIndex(1).GetInfo()); Console.WriteLine("-------Remove by id 1 from Order and Product-------"); OrderDb.RemoveById(1); ProductDb.RemoveById(1); Console.WriteLine("Orders:"); OrderDb.PrintAll(); Console.WriteLine("Products:"); ProductDb.PrintAll(); Console.ReadLine(); #endregion }