Public classZone<t>whereT:icomparable<t> { /// <summary> ///. ctor/// </summary> PublicZone () {}/// <summary> ///. ctor/// </summary> /// <param name= "briefstring" >strings, such as ("3-5")</param> PublicZone (stringbriefstring) { //T min, max; string[] arr = Briefstring.split ('-'); if(arr.) Length = =2) { if(typeof(T) = =typeof(int) ) {Min= (T) (Object)(int. Parse (arr[0])); Max= (T) (Object)(int. Parse (arr[1])); } Else if(typeof(T) = =typeof(decimal) ) {Min= (T) (Object)(decimal. Parse (arr[0])); Max= (T) (Object)(decimal. Parse (arr[1])); } Swap (); } } /// <summary> ///. ctor///Call the following///var sdsd = "3-5"; ///var zone = new Zone<int>(SDSD, p = Int.) Parse (p)); ///var zone2 = new Zone<decimal>(SDSD, p = decimal.) Parse (p)); /// </summary> /// <param name= "briefstring" >strings, such as ("3-5")</param> /// <param name= "converter" >The conversion method.</param> PublicZone (stringBriefstring, converter<string, t>Converter) { //T min, max; vararr = Briefstring.split ('-'); if(arr.) Length = =2) {Min= Converter (arr[0]); Max= Converter (arr[1]); Swap (); } } /// <summary> ///Small Value/// </summary> PublicT Min {Get;Set; } /// <summary> ///Large Value/// </summary> PublicT Max {Get;Set; } /// <summary> ///ToString ()/// </summary> /// <returns></returns> Public Override stringToString () {return string. Format ("{{min:{0},max:{1}}}", Min, Max); } /// <summary> ///returns a short string description/// </summary> /// <returns></returns> Public stringtobriefstring () {return string. Format ("{0}-{1}", Min, Max); } Private voidSwap () {if(Min.compareto (Max) >0) {T temp=Max; Max=Min; Min=temp; } } }
C # General Interval class