๊ตฌ๊ธ๋ง ํด๋ณด๋๊น C#์์ Generic Type์ย Numeric Type์ผ๋ก ์ ํํ ์๋ ์๋ค๊ณ ๋์ค๊ณ
https://stackoverflow.com/questions/32664/is-there-a-constraint-that-restricts-my-generic-method-to-numeric-types
๋๊ฐ ์ํ๋ ๊ธฐ๋ฅํ๊ฑฐ ์ง๋ดค์ผ๋๊น ์ด๊ฑธ๋ก ์ฐ๋ฉด๋์ง ์์๊น์ถ๋ค
public static bool IsInteger(ValueType value)
{
return (value is SByte || value is Int16 || value is Int32
|| value is Int64 || value is Byte || value is UInt16
|| value is UInt32 || value is UInt64
|| value is BigInteger);
}
public static bool IsFloat(ValueType value)
{
return (value is float | value is double | value is Decimal);
}
public static bool IsNumeric(ValueType value)
{
return (value is Byte ||
ย value is Int16 ||
ย value is Int32 ||
ย value is Int64 ||
ย value is SByte ||
ย value is UInt16 ||
ย value is UInt32 ||
ย value is UInt64 ||
ย value is BigInteger ||
ย value is Decimal ||
ย value is Double ||
ย value is Single);
}
public static string EmptyOrNumber(ValueType number)
{
if (number is Enum)
{
return Convert.ToInt32(number).ToString();
}
if (!IsNumeric(number))
return "";
if (IsInteger(number))
{
var i = (int)number;
if (i == 0 || i == 1)
return "";
}
else if (IsFloat(number))
{
float i = Convert.ToSingle(number);
if (i == 0 || i == 1)
{
return "";
}
}
return number.ToString();
}
ํ ์ด๋ ๊ฒ ๊น์ง ๊ด์ฌ ๊ฐ์ ธ์ค์ ๊ณ ๋ง์. ๊ทผ๋ฐ ์ด๊ฒ ์์์ ์ด 3ํญ์ฐ์ฐ์๊ฐ ๋๋ฌด ๋ฐ๋ณต ๋๋๊น. ( a == 0 || a == 1 ) ? "" : a.ToString()์ ๋์ฒด ํ๋ ค๊ณ ํ๊ฑด๋ฐ... ํ์ด ์ฌ๋ ค์ฃผ๋ ์ ๊ฒ์ ๋๋ฌด ์ฒ์์ ๋ชฉ์ ์ ๋ฒ์ด๋ ๋ฒ๋ฆฐ ๊ฒ ๊ฐ์ด. ๊ทธ๋๋ ๊ณ ๋ง์, ํ์ ์ฝ๋ ๋ณด๋ฉด์ ์ฌ๋ฌ๊ฐ์ง ์๊ฐ์ ํด๋ดค๋๋ฐ, ๊ทธ๋ฅ long ์ ์ฐ๋ฉด ๋๊ฒ ๋๋ผ.
[MethodImpl( MethodImplOption.AggressiveInline ) ] public static string EmptyOrNumber(long number) { if ( number == 0 || number == 1 ) { return ""; } else { return number.ToString(); }}์ด๋ฐ์์ผ๋ก ๋ฌด์กฐ๊ฑด long์ผ๋ก ๋ฐ์ผ๋ฉด,int๋, ๋ค๋ฅธ ์๋ฃํ๋ ๋ฌธ์ ์์ด ์ฌ์ฉํ ์ ์์ ๊ฒ ๊ฐ์.์ฌ๋ฌ๋ฒ ๋ณํ ๋ฐ ๊ณ์ฐ์ ํ๋ ๊ฒ ๋ณด๋ค ๋ฑ ํ๋ฒ (long) ์บ์คํ ํ๋๊ฒ,๊ฐ์ฅ ๋น ๋ฅด๊ณ , ํจ์ ์์ฒด๋ ๊ฐ๊ฒฐํด์ ์ข์ง ์์๊น?ํ, ์๋ฆฌ ๊ฐ์ฌํด.
๊ทธ๋ฌ๊ฒ ๋๊ฐ ํด๊ฒฐํ ๋ฐฉ๋ฒ์ด ๋ ์ข์๋ฏ. ์ด์ฝ๋ฉ ํด