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 method outside of generic class Console.WriteLine("HERE ARE THE RESULTS OF THE GENERIC METHOD"); Console.WriteLine(ReturnTypeString("Hey")); Console.WriteLine(ReturnTypeString <int>(23)); // This is also valid Console.WriteLine(ReturnTypeString(new List <bool>() { true, false })); // Generic methods in generic class // non-static calling //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); GenericListHelper <string> .GoThrough(strings); Console.ReadLine(); }
public static void GenericMethods() { //GENERIC METHODS 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 }; //non generic helpers NotGenericListHelper.GoThroughtStrings(strings); NotGenericListHelper.GetInfoForStrings(strings); Console.WriteLine("========"); NotGenericListHelper.GoThroughInts(ints); NotGenericListHelper.GetInfoForInts(ints); Console.WriteLine("========"); Console.WriteLine("========"); //generic helpers GenericListHelper.GoThrough <string>(strings); GenericListHelper.GetInfoFor <string>(strings); Console.WriteLine("========"); GenericListHelper.GoThrough <int>(ints); GenericListHelper.GetInfoFor <int>(ints); Console.WriteLine("========"); GenericListHelper.GoThrough <bool>(bools); GenericListHelper.GetInfoFor <bool>(bools); Console.WriteLine("========"); }
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 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 }
static void Main(string[] args) { #region Not generic methods NotGenericListHelper listHelper = new NotGenericListHelper(); List <string> strings = new List <string>() { "str1", "str2", "str3" }; List <int> numbers = new List <int>() { 1, 2, 3, -10 }; listHelper.GoThroughStrings(strings); listHelper.GoThroughInts(numbers); listHelper.GetInfoForStrings(strings); listHelper.GetInfoForInts(numbers); #endregion Console.WriteLine("=================== Using Generics ======================="); #region Generic methods //Methods declared in a class GenericListHelper which is a normal class //And the methods are generic and can accept all type of Lists. GenercListHelper genericListHelper = new GenercListHelper(); List <bool> checkList = new List <bool>() { true, false, true, true, true }; genericListHelper.GoThrough(strings); genericListHelper.GetInfo(strings); genericListHelper.GoThrough(numbers); genericListHelper.GetInfo(numbers); genericListHelper.GoThrough(checkList); genericListHelper.GetInfo(checkList); #endregion Console.WriteLine("=================== Using Generic class methods ======================="); #region Generic classes and methods //When using a generic class we must instantiate an object of that class //if it is a non-static class. If it is a static class we can use it by just type the name //and access the method that we want to use. Don't forget to specify the type in the < > breackets GenericClassListHelper <string> stringHelper = new GenericClassListHelper <string>(); GenericClassListHelper <int> intHelper = new GenericClassListHelper <int>(); List <string> names = new List <string>() { "Martin", "Dejan", "Ivo" }; stringHelper.GoThrough(names); stringHelper.GenericProp = "Something but must be string!"; intHelper.GoThrough(numbers); intHelper.GenericProp = 50; //Cannot call static method with an instance of the class GenericClassListHelper //stringHelper.GetInfo(); GenericClassListHelper <bool> .GetInfo(checkList); #endregion Console.WriteLine("=================== Using Generic class with constraint entity ======================="); #region Generic class with constraint entity //Cannot work since int doesn't inherit or implement BaseEntity //GenericDb<int> intDb = new GenericDb<int>(); OrderDb.Insert(new Order() { Id = 1, Reciever = "Martin", Address = "Test address number 10" }); OrderDb.Insert(new Order() { Id = 2, Reciever = "Dejan", Address = "Test address number 11" }); ProductDb.Insert(new Product() { Id = 1, Title = "Lap top", Description = "An amazing lap top!" }); ProductDb.Insert(new Product() { Id = 2, Title = "Mobile phone", Description = "An amazing mobile phone!" }); Console.WriteLine("Orders:"); OrderDb.PrintAll(); Console.WriteLine("Products:"); ProductDb.PrintAll(); Console.WriteLine("Get Order and Product by ID"); Console.WriteLine("Info about the entities:"); Console.WriteLine(OrderDb.GetById(1).GetInfo()); Console.WriteLine(ProductDb.GetById(1).GetInfo()); Console.WriteLine("Get Order and Product by Index"); Console.WriteLine(OrderDb.GetByIndex(1).GetInfo()); Console.WriteLine(ProductDb.GetByIndex(1).GetInfo()); Console.WriteLine("Remove Order and Product by Id"); OrderDb.RemoveById(1); ProductDb.RemoveById(2); Console.WriteLine("Check if itmes were removed"); OrderDb.PrintAll(); ProductDb.PrintAll(); #endregion Console.ReadLine(); }