ArrayList , List, Dictionary örnekleri içermektedir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
class Program { static void Main(string[] args) { #region ArrayList ArrayList cities = new ArrayList(); cities.Add("Adana"); cities.Add("Ankara"); foreach (var item in cities) { Console.WriteLine(item); } Console.WriteLine(cities[1]); cities.Add(1); //object tipinde ister yani bütün veri tiplerinden veri alabilmektedir. #endregion #region List -> Collections List<string> city = new List<string>(); // List -> collection'dır. <> ise Generic konusunda değinilecek. collection'ın tipini verdik burada city.Add("Ankara"); List<Customer> customers = new List<Customer>(); customers.Add(new Customer { Id = 1, Name = "Emre" }); // bu kullanımda tipini class olarak verdik. customers.Add(new Customer { Id = 2, Name = "Mert" }); Console.WriteLine(customers.Count()); // Adeti verecektir. Customer customer1 = new Customer() { Id = 3, Name = "Ali" }; customers.Add(customer1); customers.AddRange(new Customer[2] // array almaktadır. 2 elemanlı bir customer array yazıp ekleyebiliriz. { new Customer{Id=4, Name="Ayşe"}, new Customer{Id=5, Name="Fatma"} }); // customers.Clear(); // listenin bütün elemanlarını temizlemektedir. Console.WriteLine(customers.Contains(new Customer { Id = 1, Name = "Emre" })); // Bu şekilde içerde var mı diye arattığımızda aslında yeni bir new'leme yaptığımızdan sonuç false dönecektir. // Burada true dönmesi için reference göndermeliyiz. Value değil Console.WriteLine(customers.Contains(customer1)); // değer değil reference gönderdiğimizden true dönecektir. Console.WriteLine(customers.IndexOf(customer1)); // elimdeki customerın kaçıncı sırada olduğunu verecektir. 0'dan başlar index customers.LastIndexOf(customer1); // aramaya sondan başlayacaktır. customers.Insert(0, customer1); // değeri istediğimiz index'e eklememizi sağlamaktadır. customers.Remove(customer1); //verdiğimiz değeri bulup silecektir. customer1 liste'de 3 kere ekli ise 1 tanesini siler ve durur. customers.RemoveAll(c => c.Name == "Ali"); //predicate -> Name'i Ali olanların hepsini silecektir. Console.WriteLine("-------- MÜŞTERİ LİSTESİ ---------"); foreach (var customer in customers) { Console.WriteLine(customer.Name); } var a = customers.Where(p => p.Name.Contains("a")).ToList(); #endregion #region Dictionary -> Collections Console.WriteLine("-------- DICTIONARY ---------"); Dictionary<string, string> dictionary = new Dictionary<string, string>(); dictionary.Add("book", "kitap"); dictionary.Add("table", "tablo"); dictionary.Add("computer", "bilgisayar"); Console.WriteLine(dictionary["table"]); foreach (var item in dictionary) { Console.WriteLine(item); // bu şekilde key, value ikilisi birlikte verebilmektedir. Console.WriteLine(item.Key); Console.WriteLine(item.Value); // ayrı ayrı da alabilmekteyiz } // dictionary bir collection'dır. collection metotlarını kullanabiliriz. // dictionarya özel aşağıdaki metotları da vardır dictionary.ContainsKey("glass"); dictionary.ContainsValue("bardak"); #endregion } } class Customer { public int Id { get; set; } public string Name { get; set; } } |