using System.Collections.Generic;
using System.Collections;
public static void Main()
int[] awesomeNumbers = {4,2,49,21,92,39,10,08,73,94,81,95,14};
string[] emptyBasket = {};
string[] fruits = {"apple", "mango", "yakfruit", "canneryfruit", "orange", "passionfruit", "grape", "pear"};
string[] fruits2 = {"apple", "yakfruit", "orange", "grape", "pear", "watermelon"};
string[] largeFruits = {"watermelon", "papaya", "dragonfruit", "jackfruit"};
string[] grape = {"grape"};
List<object> mixedList = new List<object>();
mixedList.Add(new Product("8th Portal", 8859));
mixedList.Add(emptyArray);
mixedList.Add("Samantha");
mixedList.Add(awesomeNumbers);
mixedList.Add(new Product("5th Portal", 4929));
mixedList.Add(new Product("11th Portal", 3364));
IEnumerable<string> stringsInMixedList = mixedList.OfType<string>();
Console.WriteLine("Strings in mixedList:");
foreach (string S in stringsInMixedList){
IEnumerable<Product> productsBelow5000 = mixedList.OfType<Product>().Where(thisProduct => thisProduct.Code < 5000);
Console.WriteLine("Products with a code less than 5000:");
foreach (Product P in productsBelow5000){
Console.WriteLine("Product Name: {0} - Product Code: {1}", P.Name, P.Code);
foreach (object O in mixedList){
if (O.GetType().ToString() == "Product"){
Product oProd = (Product)O;
Console.WriteLine("object: {0} - object type: {1}", oProd.Name, O.GetType());
else if (O.GetType().ToString() == "System.Int32[]"){
Console.WriteLine("object: {0} - object type: {1}", O, O.GetType());
Console.WriteLine("---");
ArrayList fruitsFromMicrosoft = new ArrayList(4);
fruitsFromMicrosoft.Add("Mango");
fruitsFromMicrosoft.Add("Orange");
fruitsFromMicrosoft.Add("Apple");
fruitsFromMicrosoft.Add(3.0);
fruitsFromMicrosoft.Add("Banana");
IEnumerable<string> query1 = fruitsFromMicrosoft.OfType<string>();
Console.WriteLine("Elements of type 'string' are:");
foreach (string fruit in query1)
Console.WriteLine(fruit);
IEnumerable<string> query2 =
fruitsFromMicrosoft.OfType<string>().Where(fruit => fruit.ToLower().Contains("n"));
Console.WriteLine("\nThe following strings contain 'n':");
foreach (string fruit in query2)
Console.WriteLine(fruit);
public string Name{ get; set;}
public int Age{ get; set;}
public string Name{ get; set;}
public string Address{ get; set;}
public int Number{ get; set;}
public Building(string name, string addy, int num){
public string Name{ get; set;}
public int BuildingNumber{ get; set;}
public Tenant(string name, int BN){
this.BuildingNumber = BN;
public string Name { get; set; }
public int Code { get; set; }
public Product(string n, int c){
class ProductComparer : IEqualityComparer<Product>
public bool Equals(Product x, Product y)
if (Object.ReferenceEquals(x, y)) return true;
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return x.Code == y.Code && x.Name == y.Name;
public int GetHashCode(Product product)
if (Object.ReferenceEquals(product, null)) return 0;
int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();
int hashProductCode = product.Code.GetHashCode();
return hashProductName ^ hashProductCode;