public ICollection <VendorExample> GetVendors <T>(T typeOfICollectionToGet) { // simulate getting the list from the DB... if (typeOfICollectionToGet.GetType() == typeof(List <VendorExample>)) { List <VendorExample> vendorList = new List <VendorExample>(); for (int index = 0; index < 4; index++) { VendorExample vendorExample = new VendorExample(String.Format("some vendor name{0}", index.ToString())); vendorList.Add(vendorExample); } return(vendorList); } else if (typeOfICollectionToGet.GetType() == typeof(VendorExample[])) { VendorExample[] vendorsArray = new VendorExample[3]; for (int index = 0; index < vendorsArray.Length; index++) { VendorExample vendorExample = new VendorExample(String.Format("some vendor name{0}", index.ToString())); vendorsArray[index] = vendorExample; } return(vendorsArray); } for (int index = 0; index < 4; index++) { VendorExample vendorExample = new VendorExample(String.Format("some vendor name{0}", index.ToString())); } return(_iCollectionOfVendors); }
private void BuildDummyData(int numberOfRecordsToCreate) { _listOfVendors = new List <VendorExample>(); for (int index = 0; index < numberOfRecordsToCreate; index++) { VendorExample vendorExample = new VendorExample(); vendorExample.VendorName = String.Format("some vendor name{0}", index.ToString()); vendorExample.VendorID = index; vendorExample.VendorEmail = String.Format("test{0}@test.com", index.ToString()); _listOfVendors.Add(vendorExample); } }
public IEnumerable <VendorExample> GetVendorsWithIterator() { // using the yield keyword can be helpful when: // - when a method should return one element at a time (Lazy Evaluation) // - when you need a deferred execution (there is a delay in the return) // - dont use this when its not needed as it can be confusing to debug/calling code _iCollectionOfVendors = new List <VendorExample>(); for (int index = 0; index < 4; index++) { VendorExample vendorExample = new VendorExample(String.Format("some vendor name{0}", index.ToString())); _iCollectionOfVendors.Add(vendorExample); } foreach (VendorExample vendorExample in _iCollectionOfVendors) { //Console.WriteLine(vendorExample.VendorName); yield return(vendorExample); } }
private void RunGenericsOtherExamples() { Console.WriteLine("Using Interfaces as a parameter - List"); Console.WriteLine("-----------------------------------------------------"); EmailUtilitiesExample emailUtilitiesExample = new EmailUtilitiesExample(); List <VendorExample> vendorsList = new List <VendorExample>(); for (int index = 0; index < 4; index++) { VendorExample vendorExample = new VendorExample(String.Format("some vendor name{0}", index.ToString())); vendorsList.Add(vendorExample); } emailUtilitiesExample.SendVendorsEmail(vendorsList); Console.WriteLine(""); Console.WriteLine(""); //======================================================================================= //======================================================================================= Console.WriteLine("Using Interfaces as a parameter - Array"); Console.WriteLine("-----------------------------------------------------"); VendorExample[] vendorsArray = new VendorExample[3]; for (int index = 0; index < vendorsArray.Length; index++) { VendorExample vendorExample = new VendorExample(String.Format("some vendor name{0}", index.ToString())); vendorsArray[index] = vendorExample; } emailUtilitiesExample.SendVendorsEmail(vendorsArray); Console.WriteLine(""); Console.WriteLine(""); //======================================================================================= //======================================================================================= Console.WriteLine("Using Interfaces as a return type - List"); Console.WriteLine("-----------------------------------------------------"); DataBaseUtilitiesExample dataBaseUtilitiesExample = new DataBaseUtilitiesExample(); List <VendorExample> blah = dataBaseUtilitiesExample.GetVendors <List <VendorExample> >(new List <VendorExample>()).ToList(); foreach (VendorExample ex in blah) { Console.WriteLine(String.Format("vendor list: {0}", ex.VendorName)); } Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("Using Interfaces as a return type - Array"); Console.WriteLine("-----------------------------------------------------"); VendorExample[] blah2 = dataBaseUtilitiesExample.GetVendors <VendorExample[]>(new VendorExample[1]).ToArray(); foreach (VendorExample ex in blah2) { Console.WriteLine(String.Format("vendor list: {0}", ex.VendorName)); } Console.WriteLine(""); Console.WriteLine(""); //======================================================================================= //======================================================================================= Console.WriteLine("Using Yield as a return type"); Console.WriteLine("-----------------------------------------------------"); foreach (VendorExample ex in dataBaseUtilitiesExample.GetVendorsWithIterator()) { Console.WriteLine(String.Format("vendor list: {0}", ex.VendorName)); } }