// 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 WishyWashyData { // two properties // misuse of properties private int v1; public int V1AsInt { get { return v1; } set { v1 = value; } } public string V1AsString { get { return v1.ToString(); } set { v1 = int.Parse(value); } } // four methods // not the C# way private int v2; public int GetV2AsInt() { return v2; } public string GetV2AsString() { return v2.ToString(); } public void SetV2(int v) { v2 = v; } public void SetV2(string v) { v2 = int.Parse(v); } // two generic methods // overhead + not very typesafe private int v3; public T GetV3() { return (T)Convert.ChangeType(v3, typeof(T)); } public void SetV3(T v) { v3 = (int)Convert.ChangeType(v, typeof(int)); } // intermediate type with implicit conversions // lot of code + cast necessary in some cases where C# can not deduce conversion correct private int v4; public class IntOrString { private int real; private IntOrString(int real) { this.real = real; } private IntOrString(string real) { this.real = int.Parse(real); } public static implicit operator int(IntOrString v) { return v.real; } public static implicit operator string(IntOrString v) { return v.real.ToString(); } public static implicit operator IntOrString(int v) { return new IntOrString(v); } public static implicit operator IntOrString(string v) { return new IntOrString(v); } } public IntOrString V4 { get { return v4; } set { v4 = value; } } } public class Program { public static void Main(string[] args) { WishyWashyData o = new WishyWashyData(); o.V1AsString = "123"; Console.WriteLine(o.V1AsInt + 0); o.V1AsInt = 456; Console.WriteLine(o.V1AsString + 0); o.SetV2("123"); Console.WriteLine(o.GetV2AsInt() + 0); o.SetV2(456); Console.WriteLine(o.GetV2AsString() + 0); o.SetV3("123"); Console.WriteLine(o.GetV3() + 0); o.SetV3(456); Console.WriteLine(o.GetV3() + 0); o.V4 = "123"; Console.WriteLine(o.V4 + 0); o.V4 = 456; Console.WriteLine((string)o.V4 + 0); } } }