■ 제네릭 확장 메소드를 사용해 값을 비교하는 방법을 보여준다.
▶ ComparisonExtension.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System; namespace TestProject { /// <summary> /// 비교 확장 /// </summary> public static class ComparisonExtension { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 작은 값 여부 구하기 - IsSmallerThan<TSource, TTarget>(source, target) /// <summary> /// 작은 값 여부 구하기 /// </summary> /// <typeparam name="TSource">소스 타입</typeparam> /// <typeparam name="TTarget">타겟 타입</typeparam> /// <param name="source">소스 값</param> /// <param name="target">타겟 값</param> /// <returns>작은 값 여부</returns> public static bool IsSmallerThan<TSource, TTarget>(this TSource source, TTarget target) where TSource : struct, IComparable, IComparable<TSource>, IConvertible, IEquatable<TSource>, IFormattable where TTarget : struct, IComparable, IComparable<TTarget>, IConvertible, IEquatable<TTarget>, IFormattable { return Convert.ToDouble(source) < Convert.ToDouble(target); } #endregion #region 큰 값 여부 구하기 - IsLargerThan<TSource, TTarget>(source, target) /// <summary> /// 큰 값 여부 구하기 /// </summary> /// <typeparam name="TSource">소스 타입</typeparam> /// <typeparam name="TTarget">타겟 타입</typeparam> /// <param name="source">소스 값</param> /// <param name="target">타겟 값</param> /// <returns>큰 값 여부</returns> public static bool IsLargerThan<TSource, TTarget>(this TSource source, TTarget target) where TSource : struct, IComparable, IComparable<TSource>, IConvertible, IEquatable<TSource>, IFormattable where TTarget : struct, IComparable, IComparable<TTarget>, IConvertible, IEquatable<TTarget>, IFormattable { return Convert.ToDouble(source) > Convert.ToDouble(target); } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { int source = 20; int target = 25; Console.WriteLine(source.IsSmallerThan(target)); Console.WriteLine(source.IsLargerThan(target)); } #endregion } } |