2010/05/21 17:44 번역
Convert.ToInt32(), Int32.Parse() & Int32.TryParse()
* 번역된 내용은 블로거 주인장의 의견과 항상 일치하지는 않음
Int32.parse(string)
-------------------------Int32.Parse (string s) 메소드는 문자열로 표현된 수를 그에 상응하는 32비트 부호있는 정수로 변환한다.
s가 null이라면, ArgumentNullException을 던진다.
s가 정수 형태가 아닌 값이라면, FormatException을 던진다.
s가 32비트 부호있는 정수의 최소값보다 작거나 최대값보다 크다면, OverflowException을 던진다.
예:
------------------
string s1 = "1234";
string s2 = "1234.65";
string s3 = null;
string s4 = "123456789123456789123456789123456789123456789";
int result;
bool success;
result = Int32.Parse(s1); //-- 1234
result = Int32.Parse(s2); //-- FormatException
result = Int32.Parse(s3); //-- ArgumentNullException
result = Int32.Parse(s4); //-- OverflowException
Convert.ToInt32(string)
----------------------------------
Convert.ToInt32(string s) 메소드는 문자열로 표현된 수를 그에 상응하는 32비트 부호있는 정수로 변환한다. 내부에서는 Int32.Parse() 메소드를 호출한다.
예:
result = Convert.ToInt32(s1); //-- 1234
result = Convert.ToInt32(s2); //-- FormatException
result = Convert.ToInt32(s3); //-- 0
result = Convert.ToInt32(s4); //-- OverflowException
Int32.TryParse(string, out int)
---------------------------------------------
Int32.TryParse(string, out int) 메소드는 문자열로 표현된 수를 그에 상응하는 32비트 부호있는 정수로 변환하여 out 변수에 저장한 후 성공적으로 변환했으면 true를, 그렇지 않으면 false를 반환한다. 이 메소드는 C# 2.0 이상부터 사용할 수 있다.
예:
-------------
success = Int32.TryParse(s1, out result); //-- success => true; result => 1234
success = Int32.TryParse(s2, out result); //-- success => false; result => 0
success = Int32.TryParse(s3, out result); //-- success => false; result => 0
success = Int32.TryParse(s4, out result); //-- success => false; result => 0
예:
------------------
string s1 = "1234";
string s2 = "1234.65";
string s3 = null;
string s4 = "123456789123456789123456789123456789123456789";
int result;
bool success;
result = Int32.Parse(s1); //-- 1234
result = Int32.Parse(s2); //-- FormatException
result = Int32.Parse(s3); //-- ArgumentNullException
result = Int32.Parse(s4); //-- OverflowException
Convert.ToInt32(string)
----------------------------------
Convert.ToInt32(string s) 메소드는 문자열로 표현된 수를 그에 상응하는 32비트 부호있는 정수로 변환한다. 내부에서는 Int32.Parse() 메소드를 호출한다.
s가 null이라면, ArgumentNullException을 던지지 않고 0을 반환한다.
s가 정수 형태가 아닌 값이라면, FormatException을 던진다.
s가 32비트 부호있는 정수의 최소값보다 작거나 최대값보다 크다면, OverflowException을 던진다.
예:
result = Convert.ToInt32(s1); //-- 1234
result = Convert.ToInt32(s2); //-- FormatException
result = Convert.ToInt32(s3); //-- 0
result = Convert.ToInt32(s4); //-- OverflowException
Int32.TryParse(string, out int)
---------------------------------------------
Int32.TryParse(string, out int) 메소드는 문자열로 표현된 수를 그에 상응하는 32비트 부호있는 정수로 변환하여 out 변수에 저장한 후 성공적으로 변환했으면 true를, 그렇지 않으면 false를 반환한다. 이 메소드는 C# 2.0 이상부터 사용할 수 있다.
s가 null이라면, ArgumentNullException을 던지지 않고 0을 얻게 된다.
s가 정수 형태가 아닌 값이라면, FormatException을 던지지 않고 0을 얻게 된다.
s가 32비트 부호있는 정수의 최소값보다 작거나 최대값보다 크다면, OverflowException을 던지지 않고 0을 얻게 된다.
예:
-------------
success = Int32.TryParse(s1, out result); //-- success => true; result => 1234
success = Int32.TryParse(s2, out result); //-- success => false; result => 0
success = Int32.TryParse(s3, out result); //-- success => false; result => 0
success = Int32.TryParse(s4, out result); //-- success => false; result => 0
예외를 던지기 보다는 0을 반환하기 때문에 Convert.ToInt32가 Int32.parse보다 낫다. 그러나, 요구 조건에 따라 사용할 수도 있다. TryParse가 언제나 예외를 스스로 처리하기 때문에 가낭 나을 것이다.
'번역' 카테고리의 다른 글
| Convert.ToInt32(), Int32.Parse() & Int32.TryParse() (0) | 2010/05/21 |
|---|---|
| 멀티쓰레딩 어플리케이션에 관해 모든 개발자가 알아야만 하는 것들 - (1) (0) | 2008/09/08 |
| 틈틈히 번역된 문서를 올려 보려고 합니다. (3) | 2008/01/27 |