using System; // For Console class, and IComparable interface namespace DemoTypes { public struct Money : IComparable { // Private fields. private int mCentsAmount; private const string mCurrencySymbol = "$"; // Public constructor. public Money(int dollars, int cents) { mCentsAmount = (dollars * 100) + cents; } // Another public constructor. public Money(double amount) { mCentsAmount = (int)(amount * 100); } // Return a string representation of Money object. public override string ToString() { return String.Format("{0}{1:f2}", mCurrencySymbol, mCentsAmount / 100.0); } // Compare with another Money object. public int CompareTo(Money other) { if (this.mCentsAmount < other.mCentsAmount) return -1; else if (this.mCentsAmount == other.mCentsAmount) return 0; else return +1; } } static class StructuresDemo { public static void DoDemo() { Console.WriteLine("\nStructuresDemo"); Console.WriteLine("------------------------------------------------------"); Money[] values = new Money[5]; values[0] = new Money(9, 50); values[1] = new Money(4, 80); values[2] = new Money(8, 70); values[3] = new Money(2.50); values[4] = new Money(6); Console.WriteLine("Unsorted array:"); foreach (Money v in values) { Console.WriteLine("{0}", v); } Array.Sort(values); Console.WriteLine("Sorted array:"); foreach (Money v in values) { Console.WriteLine("{0}", v); } } } }