0% found this document useful (0 votes)
38 views

Foreach Int in Yield Return: (I Data) I

The document defines several classes related to enumerables and comparables in C#: 1. MyIntList implements IEnumerable<int> and defines an inner Enumerator class to iterate over its integer data array. 2. MyGenCollection also implements IEnumerable<int> but uses yield return to generate its enumerator instead of a separate class. 3. Test demonstrates using yield return to define an IEnumerable<int> directly without a separate class. 4. Entliczek2 implements IEnumerable<char> to iterate over a string. 5. Various examples show implementing IComparable and IComparer interfaces to allow sorting by properties like temperature or duck weight

Uploaded by

anon_394655724
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Foreach Int in Yield Return: (I Data) I

The document defines several classes related to enumerables and comparables in C#: 1. MyIntList implements IEnumerable<int> and defines an inner Enumerator class to iterate over its integer data array. 2. MyGenCollection also implements IEnumerable<int> but uses yield return to generate its enumerator instead of a separate class. 3. Test demonstrates using yield return to define an IEnumerable<int> directly without a separate class. 4. Entliczek2 implements IEnumerable<char> to iterate over a string. 5. Various examples show implementing IComparable and IComparer interfaces to allow sorting by properties like temperature or duck weight

Uploaded by

anon_394655724
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

class MyIntList : IEnumerable<int>

{
int[] data = { 1, 2, 3 };
// The generic enumerator is compatible with both IEnumerable and
// IEnumerable<T>. We implement the nongeneric GetEnumerator method
// explicitly to avoid a naming conflict.
public IEnumerator<int> GetEnumerator()
{ return new Enumerator(this); }
IEnumerator IEnumerable.GetEnumerator()
{ return new Enumerator(this); }
class Enumerator : IEnumerator<int>
{
int currentIndex = -1;
MyIntList collection;
public Enumerator(MyIntList collection)
{
this.collection = collection;
}
public int Current { get { return collection.data[currentIndex]; } }
object IEnumerator.Current { get { return Current; } }
public bool MoveNext()
{
return ++currentIndex < collection.data.Length;
}
public void Reset() { currentIndex = -1; }
// Given we don't need a Dispose method, it's good practice to
// implement it explicitly, so it's hidden from the public interface.
void IDisposable.Dispose() { }
}

}
*********************************
public class MyGenCollection : IEnumerable<int>
{
int[] data = { 1, 2, 3 };
public IEnumerator<int> GetEnumerator()
{
foreach (int i in data)
yield return i;
}
IEnumerator IEnumerable.GetEnumerator() // Explicit implementation
{
// keeps it hidden.
return GetEnumerator();
}
}
************************************
public class Test
{
public static IEnumerable<int> GetSomeIntegers()
{
yield return 1;
yield return 2;
yield return 3;
}
}
-foreach (int i in Test.GetSomeIntegers())
Console.WriteLine(i);
1
2
3
class Entliczek2 : IEnumerable<char>

string tabliczka = "ABCdefghaaijk";


public IEnumerator<char> GetEnumerator()
{
return tabliczka.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}

Sorting

int[] numerki = { 6,7, 1, 2, 3, 4, 5 };


Array.Sort(numerki, (x, y) => { if (x > y) return -1; else if (x < y) return 1; else return
0; });

public class Temperature : IComparable


{
protected double tempF;
public int CompareTo(object obj)
{
if (obj == null) return 1;

Temperature otherTemperature = obj as Temperature;


if (otherTemperature != null)
return this.tempF.CompareTo(otherTemperature.tempF);
else
throw new ArgumentException("Object is not a Temperature");

}
public int CompareTo(object obj)
{
if (obj == null) return 1;
Temperature otherTemperature = obj as Temperature;
if (otherTemperature != null)
if(this.tempF>otherTemperature.tempF)
return 1;
else if( this.tempF<otherTemperature.tempF)
return-1;
else
return 0;
else
throw new ArgumentException("Object is not a Temperature");

public class Temperature : IComparable<Temperature>


{
public double tempF;
public int CompareTo(Temperature obj)
{
if (obj == null) return 1;
if (this.tempF > obj.tempF)
return 1;
else if (this.tempF < obj.tempF)
return -1;
else
return 0;
}
}

IComparer

class Kaczka
{
public int Wiek { get; set; }
public int Waga { get; set; }
}
class KaczkaComapreByWaga : IComparer<Kaczka>
{
int IComparer<Kaczka>.Compare(Kaczka x, Kaczka y)
{
if (x.Waga < y.Waga)
return -1;
else if (x.Waga < y.Waga)
return 1;
else
return 0;
}
class KaczkaCompareByWiek : IComparer<Kaczka>
{
public int Compare(Kaczka x, Kaczka y)
{
return x.Wiek.CompareTo(y.Wiek);
}
}
List<Kaczka> kaczaList = new List<Kaczka>
{ new Kaczka { Wiek = 12, Waga = 13 },
new Kaczka { Wiek = 15, Waga = 11 },
new Kaczka { Wiek = 1, Waga = 14 },
new Kaczka { Wiek = 18, Waga = 19 },
new Kaczka { Wiek = 11, Waga = 4 }
};
kaczaList.Sort(new KaczkaCompareByWiek());
foreach (var k in kaczaList)
Console.WriteLine("Wiek: {0}, Waga: {1},", k.Wiek, k.Waga);
class Kaczka
{
public int Wiek { get; set; }
public int Waga { get; set; }

public static IComparer<Kaczka> ByWiek


{
get { return new KaczkaCompareByWiek(); }
}

}
kaczaList.Sort(Kaczka.ByWiek);
Array.Sort(tablicaDoPosortowania, new IComparerClass());

You might also like