|
1 |
| -// See https://aka.ms/new-console-template for more information |
2 |
| -Console.WriteLine("Hello, World!"); |
| 1 | + |
| 2 | +using System.Collections; |
| 3 | +using System.ComponentModel; |
| 4 | + |
| 5 | +Console.WriteLine(); |
| 6 | +#region IComparer Interface |
| 7 | +/* |
| 8 | +//IComparer interface is used to compare two objects of the same type. |
| 9 | +
|
| 10 | +Person p1 = new Person { Name = "Hakan", Age = 22 }; |
| 11 | +Person p2 = new Person { Name = "Alperen", Age = 22 }; |
| 12 | +
|
| 13 | +AgeComparer comparer = new AgeComparer(); |
| 14 | +int result = comparer.Compare(p1, p2); |
| 15 | +Console.WriteLine(result); // 0, 1 if p1 is greater, -1 if p2 is greater |
| 16 | +
|
| 17 | +// Sort the list of persons by age |
| 18 | +List<Person> persons = new List<Person> |
| 19 | +{ |
| 20 | + new Person { Name = "Hakan", Age = 22 }, |
| 21 | + new Person { Name = "Alperen", Age = 22 }, |
| 22 | + new Person { Name = "Mehmet", Age = 20 }, |
| 23 | + new Person { Name = "Ahmet", Age = 25 } |
| 24 | +}; |
| 25 | +
|
| 26 | +persons.Sort(new AgeComparer()); |
| 27 | +foreach (var person in persons) |
| 28 | +{ |
| 29 | + Console.WriteLine($"{person.Name} - {person.Age}"); |
| 30 | +} // Mehmet - 20, Hakan - 22, Alperen - 22, Ahmet - 25 |
| 31 | +
|
| 32 | +public class AgeComparer : IComparer<Person> |
| 33 | +{ |
| 34 | + public int Compare(Person? x, Person? y) |
| 35 | + { |
| 36 | + return x?.Age.CompareTo(y?.Age) ?? 0; |
| 37 | + //return y?.Age.CompareTo(x?.Age) ?? 0; |
| 38 | +
|
| 39 | + } |
| 40 | +} |
| 41 | +
|
| 42 | +public class Person |
| 43 | +{ |
| 44 | + public string Name { get; set; } |
| 45 | + public int Age { get; set; } |
| 46 | +} |
| 47 | +*/ |
| 48 | + |
| 49 | +#endregion |
| 50 | + |
| 51 | + |
| 52 | +#region IComparable Interface |
| 53 | +/* |
| 54 | +//IComparable interface is used to compare two objects. |
| 55 | +
|
| 56 | +Person p1 = new Person { Name = "Hakan", Age = 22 }; |
| 57 | +Person p2 = new Person { Name = "Alperen", Age = 25 }; |
| 58 | +
|
| 59 | +int result = p1.CompareTo(p2); |
| 60 | +Console.WriteLine(result); // -1, 0 if p1 is equal, 1 if p1 is greater |
| 61 | +public class Person : IComparable<Person> |
| 62 | +{ |
| 63 | + public string Name { get; set; } |
| 64 | + public int Age { get; set; } |
| 65 | +
|
| 66 | + public int CompareTo(Person? other) |
| 67 | + { |
| 68 | + return Age.CompareTo(other?.Age); |
| 69 | + |
| 70 | + } |
| 71 | +} |
| 72 | +*/ |
| 73 | +#endregion |
| 74 | + |
| 75 | +#region ICloneable Interface |
| 76 | +//ICloneable interface is used to clone a instance. Cloning is the process of creating a new object that is a copy of the current instance. |
| 77 | +// For example if constructor takes lots of parameters, it is better to use ICloneable interface. |
| 78 | +// Because it is not good to pass all parameters to the constructor again while creating a new instance. |
| 79 | + |
| 80 | +/* |
| 81 | +Person p1 = new Person { Name = "Hakan", Age = 22 }; |
| 82 | +Person p2 = (Person)p1.Clone(); |
| 83 | +
|
| 84 | +foreach (Person person in new List<Person> { p1, p2 }) |
| 85 | +{ |
| 86 | + Console.WriteLine($"{person.Name} - {person.Age}"); // Hakan - 22, Hakan - 22 |
| 87 | +} |
| 88 | +
|
| 89 | +// Even thoguh it's powerful, it is not recommended to use ICloneable interface. More secure and better way is to use prototype pattern. |
| 90 | +
|
| 91 | +
|
| 92 | +public class Person : ICloneable |
| 93 | +{ |
| 94 | + public string Name { get; set; } |
| 95 | + public int Age { get; set; } |
| 96 | +
|
| 97 | + public object Clone() |
| 98 | + { |
| 99 | + return this.MemberwiseClone(); |
| 100 | + } |
| 101 | +} |
| 102 | +*/ |
| 103 | +#endregion |
| 104 | + |
| 105 | + |
| 106 | +#region INotifyPropertyChanged Interface |
| 107 | +//INotifyPropertyChanged interface is used to notify the subscribers when a property is changed. Is generally used in data binding scenarios. |
| 108 | +/* |
| 109 | +Person p = new Person { Name = "Hakan", Age = 22 }; |
| 110 | +
|
| 111 | +p.PropertyChanged += (sender, e) => |
| 112 | +{ |
| 113 | + Console.WriteLine($"{e.PropertyName} has been changed."); |
| 114 | +}; |
| 115 | +
|
| 116 | +p.Age = 25; // Age has been changed. |
| 117 | +public class Person : INotifyPropertyChanged |
| 118 | +{ |
| 119 | + public string Name { get; set; } |
| 120 | + int age; |
| 121 | +
|
| 122 | + public int Age |
| 123 | + { |
| 124 | + get => age; |
| 125 | + set |
| 126 | + { |
| 127 | + age = value; |
| 128 | + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Age))); |
| 129 | +
|
| 130 | + } |
| 131 | + } |
| 132 | + public event PropertyChangedEventHandler? PropertyChanged; |
| 133 | +} |
| 134 | +*/ |
| 135 | +#endregion |
| 136 | + |
| 137 | + |
| 138 | +#region IEquatable Interface |
| 139 | +//Records can be used instead of IEquatable interface. |
| 140 | +/* |
| 141 | +Person p1 = new Person { Name = "Hakan", Age = 22 }; |
| 142 | +Person p2 = new Person { Name = "Hakan", Age = 23 }; |
| 143 | +
|
| 144 | +Console.WriteLine(p1.Equals(p2)); // False |
| 145 | +
|
| 146 | +p2.Age = 22; |
| 147 | +
|
| 148 | +Console.WriteLine(p1.Equals(p2)); // True |
| 149 | +
|
| 150 | +class Person : IEquatable<Person> |
| 151 | +{ |
| 152 | + public string Name { get; set; } |
| 153 | + public int Age { get; set; } |
| 154 | +
|
| 155 | + public bool Equals(Person? other) |
| 156 | + { |
| 157 | + return Name == other?.Name && Age == other?.Age; |
| 158 | + } |
| 159 | +} |
| 160 | +*/ |
| 161 | +#endregion |
| 162 | + |
| 163 | +#region IEnumerable Interface |
| 164 | +/* |
| 165 | +//IEnumerable interface gives ability to iterate over a collection. (foreach) |
| 166 | +
|
| 167 | +ArrayList list = new(); |
| 168 | +
|
| 169 | +foreach (var item in list) // No error |
| 170 | +{ |
| 171 | + Console.WriteLine(item); |
| 172 | +} |
| 173 | +
|
| 174 | +Market market = new(); |
| 175 | +foreach (var item in market) // Foreach statement cannot operate on variables of type 'Market' because 'Market' does not contain a public instance definition for 'GetEnumerator' (without implementing IEnumerable interface) GetEnumerator method is required to use foreach. |
| 176 | +{ |
| 177 | + Console.WriteLine(item); // Apple, Banana, Orange |
| 178 | +} |
| 179 | +
|
| 180 | +
|
| 181 | +var result = market.Where(p => p.Contains("o", StringComparison.InvariantCultureIgnoreCase)).ToList(); |
| 182 | +
|
| 183 | +foreach (var item in result) |
| 184 | +{ |
| 185 | + Console.WriteLine(item); // Orange |
| 186 | +} |
| 187 | +
|
| 188 | +public class Market : IEnumerable<string> // No need to implement IEnumerable interface if GetEnumerator method is available. |
| 189 | +{ |
| 190 | + List<string> list = new List<string>() { "Apple", "Banana", "Orange" }; |
| 191 | +
|
| 192 | + //If there is no GetEnumerator method, it will give an error. |
| 193 | + public IEnumerator GetEnumerator() |
| 194 | + { |
| 195 | + return list.GetEnumerator(); |
| 196 | + } |
| 197 | +
|
| 198 | + IEnumerator<string> IEnumerable<string>.GetEnumerator() |
| 199 | + { |
| 200 | + return list.GetEnumerator(); |
| 201 | + } |
| 202 | +} |
| 203 | +*/ |
| 204 | +#endregion |
| 205 | + |
| 206 | +#region IDisposable Interface |
| 207 | +//IDisposable interface is used to release unmanaged resources. It gives opportunity to clean up resources when they are no longer needed by using "using" keyword. |
| 208 | + |
| 209 | +class Database : IDisposable |
| 210 | +{ |
| 211 | + //connection, command , reader etc. |
| 212 | + public void Dispose() |
| 213 | + { |
| 214 | + //nullify the resources |
| 215 | + Console.WriteLine("Database is disposed."); |
| 216 | + } |
| 217 | + |
| 218 | + ~Database() |
| 219 | + { |
| 220 | + this.Dispose(); |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | + |
| 225 | +#endregion |
0 commit comments