public class Person : IEquatable<Person>,IComparable<Person>
{
public int Id { get; private set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return String.Format("{0}, {1} {2}", Id, FirstName, LastName);
}
public bool Equals(Person other)
{
if (other == null)
return base.Equals(other);
return this.FirstName == other.FirstName && this.LastName == other.LastName;
}
public int CompareTo(Person other)
{
if (other == null) throw new ArgumentNullException("other");
int result = this.LastName.CompareTo(other.LastName);
if (result == 0)
{
result = this.FirstName.CompareTo(other.FirstName);
}
return result;
}
}
Person[] persons = {
new Person { FirstName = "Simen03", LastName = "Go" },
new Person { FirstName = "Simen02", LastName = "Go" },
new Person { FirstName = "Simen01", LastName = "Go" }
};
Array.Sort(persons);
foreach (var person in persons)
Console.WriteLine(person);
// loop start (head: IL_009b) IL_008a: ldloc.2 IL_008b: ldloc.3 IL_008c: ldelem.ref IL_008d: stloc.s person IL_008f: ldloc.s person IL_0091: call void [mscorlib]System.Console::WriteLine(object) IL_0096: nop IL_0097: ldloc.3 IL_0098: ldc.i4.1 IL_0099: add IL_009a: stloc.3 IL_009b: ldloc.3 IL_009c: ldloc.2 IL_009d: ldlen IL_009e: conv.i4 IL_009f: blt.s IL_008a // end loop
IEnumerator enumerator = persons.GetEnumerator();
while (enumerator.MoveNext())
{
var person = enumerator.Current;
Console.WriteLine(person);
}
static void Main(string[] args)
{
var helloCollection = new HelloCollection();
foreach (string s in helloCollection)
{
Console.WriteLine(s);
}
}
public class HelloCollection
{
public IEnumerator<string> GetEnumerator()
{
yield return "Hello";
yield return "World";
}
}
public class HelloCollectionOther
{
public IEnumerator GetEnumertor()
{
return new Enumerator(0);
}
public class Enumerator : IEnumerator<string>, IEnumerator, IDisposable
{
private int state;
private string current;
public Enumerator(int state)
{
this.state = state;
}
public string Current => throw new NotImplementedException();
object IEnumerator.Current
{
get { return current; }
}
public void Dispose()
{
throw new NotImplementedException();
}
public bool MoveNext()
{
switch (state)
{
case 0:current = "hello";
state = 1;
return true;
case 1:current = "world";
state = 2;
return true;
case 2:
break;
}
return false;
}
public void Reset()
{
throw new NotImplementedException();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有