Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implimentering af IComparable interfacet class Cirkel: IComparable { private long radius; // object memory :::::::::::::::::::::::::::::::::::::: #region.

Similar presentations


Presentation on theme: "Implimentering af IComparable interfacet class Cirkel: IComparable { private long radius; // object memory :::::::::::::::::::::::::::::::::::::: #region."— Presentation transcript:

1 Implimentering af IComparable interfacet class Cirkel: IComparable { private long radius; // object memory :::::::::::::::::::::::::::::::::::::: #region IComparable Members // returner < 0 hvis this er mindst // returner 0 hvis ens // returner > 0 hvis this er størst public int CompareTo(object obj) { return (int)(this.radius - (obj as Cirkel).radius); } #endregion }

2 Brug af IComparable interfacet til sortering Console.WriteLine("\nstatic void AfprøvCirkelSortering()"); List l = new List (); l.Add(new Cirkel(10)); l.Add(new Cirkel(11)); l.Add(new Cirkel(9)); l.Add(new Cirkel(8)); l.Add(new Cirkel(20)); Console.WriteLine("\nListen som den er"); foreach (Cirkel c in l) { Console.WriteLine("Radius:" + c.Radius); Console.WriteLine("Omkreds:" + c.Omkreds()); Console.WriteLine("Areal:" + c.Areal()); } Console.WriteLine("\nListen sorteret og reverse"); l.Sort(); l.Reverse(); foreach (ICirkel c in l) { Console.WriteLine("Radius:" + c.Radius); Console.WriteLine("Omkreds:" + c.Omkreds()); Console.WriteLine("Areal:" + c.Areal()); }

3 Brug af IComparable interfacet til sortering Console.WriteLine("\nListen kopiet til array og sorteret"); Cirkel[] ac = l.ToArray(); Array.Sort(ac); foreach (Cirkel c in ac) { Console.WriteLine("Radius:" + c.Radius); Console.WriteLine("Omkreds:" + c.Omkreds()); Console.WriteLine("Areal:" + c.Areal()); }


Download ppt "Implimentering af IComparable interfacet class Cirkel: IComparable { private long radius; // object memory :::::::::::::::::::::::::::::::::::::: #region."

Similar presentations


Ads by Google