Programm.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ExtensionsMethods { class Program { static void DoWork() { int x = 591; for ( int i = 2; i <= 10; i++) { Console.WriteLine( " {0} in base {1} is {2} ",x,i,x.ConvertToBase(i)); } } static void Main( string[] args) { try { DoWork(); } catch(Exception ex) { Console.WriteLine(ex.Message); } } } }
Util.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ExtensionsMethods { static class Util { public static int ConvertToBase( this int i, int baseToConvertTo) { if (baseToConvertTo < 2 || baseToConvertTo > 10) { throw new ArgumentException( " Value cannot be converted to base "+baseToConvertTo.ToString()); } int result = 0; int iterations = 0; do { int nextDigit = i % baseToConvertTo; i /= baseToConvertTo; result += nextDigit * ( int)Math.Pow( 10, iterations); iterations++; } while (i != 0); return result; } } }