Last active
May 26, 2016 10:38
-
-
Save rohinomiya/3452942 to your computer and use it in GitHub Desktop.
ジェネリックと拡張メソッドを使ってみる ref: http://qiita.com/rohinomiya/items/1aa08c088a62f46d9fe1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class MyGeneral // 静的クラス | |
{ | |
// 静的クラスなのでコンストラクタは無し | |
/// <summary> | |
/// 数値 current がlower ~ higher の範囲内か?(Generic版) | |
/// </summary> | |
/// <param name="lower">区間(開始)</param> | |
/// <param name="current">比較される値</param> | |
/// <param name="higher">区間(終了)</param> | |
/// <param name="inclusive">閉区間か?(境界値を含む) (初期値:true)</param> | |
/// <returns>範囲内であればtrue</returns> | |
public static bool isBetween<T>(this T current, T lower, T higher, bool inclusive = true) where T : IComparable | |
{ | |
// 拡張メソッドは1つ目の引数に this キーワードを付ける | |
// ジェネリックだと比較演算子が使えなくなってしまうので、 | |
// where句 で型パラメーター T が IComparable<T> インターフェイスを実装するように指定 | |
// CompareTo() メソッドが使えるようになる。 | |
if(lower.CompareTo(higher) > 0 ) Swap(ref lower,ref higher); | |
return inclusive ? | |
(lower.CompareTo(current) <= 0 && current.CompareTo(higher) <= 0 ) : | |
(lower.CompareTo(current) < 0 && current.CompareTo(higher) < 0 ); | |
} | |
/// <summary> | |
/// 2つの値a,bを交換する | |
/// </summary> | |
/// <param name="a">値A</param> | |
/// <param name="b">値B</param> | |
public static void Swap<T>(ref T a, ref T b) | |
{ | |
T temp = a; | |
a = b; | |
b = temp; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool inRange; | |
inRange = 2.isBetween(1,3); // true | |
inRange = "b".isBetween("a","c"); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment