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(); }
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 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 }
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(); }