//---------------------------------------------------------------------------- // Copyright (C) 2013-2015 Fabrice HARROUET (ENIB) // // Permission to use, copy, modify, distribute and sell this software // and its documentation for any purpose is hereby granted without fee, // provided that the above copyright notice appear in all copies and // that both that copyright notice and this permission notice appear // in supporting documentation. // The author makes no representations about the suitability of this // software for any purpose. // It is provided "as is" without express or implied warranty. //---------------------------------------------------------------------------- using System; public class Test { public static void swap(ref T x, ref T y) { Console.WriteLine("swaping {0} and {1} ({2})", x,y,typeof(T)); T tmp=x; x=y; y=tmp; } public static T clamp(T minValue, T value, T maxValue) where T : System.IComparable { Console.WriteLine("clamping {0} between {1} and {2} ({3})", value,minValue,maxValue,typeof(T)); return value.CompareTo(minValue)<0 ? minValue : value.CompareTo(maxValue)>0 ? maxValue : value; } public static void Main(string[] args) { int i=123; int j=456; Console.WriteLine("before: i={0} j={1}",i,j); swap(ref i,ref j); Console.WriteLine("after: i={0} j={1}",i,j); float u=11.22f; float v=33.44f; Console.WriteLine("before: u={0} v={1}",u,v); swap(ref u,ref v); Console.WriteLine("after: u={0} v={1}",u,v); Console.WriteLine("--> {0}",clamp(2,4,3)); Console.WriteLine("--> {0}",clamp(2,4.5,3)); Console.WriteLine("--> {0}",clamp("coucou","salut","hello")); } } //----------------------------------------------------------------------------