[Unity 기초] Int32.Parse VS Convert.Toint32

배근철·2022년 11월 14일

Unity 기초

목록 보기
9/10

✍ Int32.Parse(string)

string s1 = "1234";
string s2 = "1234.65";
string s3 = null;
string s4 = "123456789123456789123456789123456798123456789";
  • 위의 s1~s4를 Int32.Parse로 컨버팅하면 s1은 1234로 정상출력되지만 s2~s4는 에러를 발생시킨다.
  • s2 : FormatException
  • s3 : ArgumentNullException
  • s4 : OverflowException

✍ Convert.ToInt32(string)

  • s1~s4를 Convert.ToInt32로 컨버팅하면 s1은 1234 s3는 0으로 출력되고
  • s2와 s3는 Int32.Parse와 같은 에러를 출력한다.

✍ Int32.TrayParse(string, out int)

  • s2~s4를 모두 0으로 출력

결론

  • 파라미터가 null일때, 예외처리를 하느냐 0으로 리턴하느냐 차이가 있다.

0개의 댓글