// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class DateTimeOrString { private DateTime dt; private DateTimeOrString(DateTime dt) { this.dt = dt; } private DateTime Get() { return dt; } public static implicit operator DateTimeOrString(DateTime dt) { return new DateTimeOrString(dt); } public static implicit operator DateTimeOrString(String s) { return new DateTimeOrString(DateTime.Parse(s)); } public static implicit operator DateTime(DateTimeOrString dts) { return dts.Get(); } public static implicit operator String(DateTimeOrString dts) { return dts.Get().ToString(); } } public class Foobar { public DateTimeOrString TheTime { get; set; } } public class Program { public static void Main(string[] args) { Foobar o = new Foobar(); o.TheTime = DateTime.Now; String s = o.TheTime; o.TheTime = s; DateTime dt = o.TheTime; Console.WriteLine("{0} {1}", s, dt); } } }