CSharp examples for Custom Type:Operator Overloading
Implicit Conversion via implicit operator
using System;/*from w w w . j a v a 2 s .c o m*/ class TimeSpan { private uint totalSeconds; public TimeSpan(uint initialTotalSeconds) { totalSeconds = initialTotalSeconds; } public void PrintSeconds() { Console.WriteLine("Total seconds: {0}", totalSeconds); } public static implicit operator TimeSpan(uint convertFrom) { TimeSpan newTimeSpan; newTimeSpan = new TimeSpan(convertFrom); Console.WriteLine("Converting from uint to TimeSpan"); return newTimeSpan; } } class ImplicitConversionTester { public static void Main() { uint simpleTimeSeconds = 30; TimeSpan myTime; myTime = simpleTimeSeconds; myTime.PrintSeconds(); } }