using System;
using System.Collections;
namespace CodeInConsole
{
public class Solution
{
public static void Main()
{
Console.WriteLine("");
Console.WriteLine("---");
Console.WriteLine("SortedList1 örneği");
Console.WriteLine("---");
Console.WriteLine("");
// Creates and initializes a new SortedList.
SortedList sortedList = new SortedList();
sortedList.Add("Third", "!");
sortedList.Add("Second", "World");
sortedList.Add("First", "Hello");
// Displays the properties and values of the SortedList.
Console.WriteLine(" Count: {0}", sortedList.Count);
Console.WriteLine(" Capacity: {0}", sortedList.Capacity);
Console.WriteLine(" Keys and Values:");
PrintKeysAndValues(sortedList);
}
public static void PrintKeysAndValues(SortedList myList)
{
// \t --> empty space yapıyor, tab'a basılıp yazmaya başlanmış gibi.
Console.WriteLine("\t-KEY-\t-VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine("\t{0}:\t{1}", myList.GetKey(i),
myList.GetByIndex(i));
}
Console.WriteLine("");
Console.WriteLine("---");
Console.WriteLine("SortedList2 örneği");
Console.WriteLine("---");
Console.WriteLine("");
SortedList sortedList2 = new SortedList();
// sortedList kapasitesi şu an 0
Console.WriteLine("Capacity: " + sortedList2.Capacity);
sortedList2.Add("key1", "val1");
sortedList2.Add("key2", "val2");
sortedList2.Add("key3", "val3");
// sortedlist kapasitesi şu an 16 (veri eklendikten
// sonra otomatik olarak 16'ya çıktı)
Console.WriteLine("Capacity: " + sortedList2.Capacity);
// System.InvalidOperationException: 'Failed to compare two
// elements in the array.' ArgumentException: Object must be of type String.
// sortedList2.Add(2, 5);
// System.InvalidOperationException: 'Failed to compare
// two elements in the array.' ArgumentException: Object must be of type String.
//sortedList2.Add(true, false);
// Ayrıca: key'ler özel olmalıdır. Aynı isimde
// iki key değeri girilemiyor, hata veriyor.
for (int i = 0; i < sortedList2.Count; i++){
Console.WriteLine("Key: " + sortedList2.GetKey(i)
+ "\t" + "Value: " + sortedList2.GetByIndex(i));
}
Console.WriteLine("");
Console.WriteLine("---");
Console.WriteLine("SortedList3 örneği");
Console.WriteLine("---");
Console.WriteLine("");
SortedList sortedList3 = new SortedList();
sortedList3.Add(true, false);
sortedList3.Add(false, true);
// Yukarıda bool type'lar eklendi sortedList3'e.
// Aşağıda ise aynı listeye string eklendiğinde hata oluşuyor:
// System.InvalidOperationException:
// 'Failed to compare two elements in the array.'
//sortedList3.Add("text1", "text2");
for (int i = 0; i < sortedList3.Count; i++)
{
Console.WriteLine("Key: " + sortedList3.GetKey(i)
+ "\t" + "Value: " + sortedList3.GetByIndex(i));
}
}
}
}
Hiç yorum yok:
Yorum Gönder