{"id":136,"date":"2014-08-08T12:54:11","date_gmt":"2014-08-08T12:54:11","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=136"},"modified":"2014-08-08T12:54:11","modified_gmt":"2014-08-08T12:54:11","slug":"difference-between-convertexplicit-typetryparse-and-parse-c","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=136","title":{"rendered":"Difference between Convert,Explicit type,Tryparse and Parse c#"},"content":{"rendered":"<p>(int)Something;<\/p>\n<p>int.Parse(Something);<\/p>\n<p>Convert.ToInt32(Something);<\/p>\n<p>1) that is a cast<\/p>\n<p>2) Parsing taking in a string and attempts to convert it to a type. &#8212;when string is null refence,it will throw ArgumentNullException.if string is other than integer value ,it will throw FormatException.if reperesents a number less than Min or maxvalue than will throw overflowexception.<\/p>\n<p>string s1 = &#8220;1234&#8221;;<br \/>\nstring s2 = &#8220;1234.65&#8221;;<br \/>\nstring s3 = null;<br \/>\nstring s4 = &#8220;123456789123456789123456789123456789123456789&#8221;;<\/p>\n<p>int result;<br \/>\nbool success;<\/p>\n<p>result = Int32.Parse(s1); \/\/&#8211; 1234<br \/>\nresult = Int32.Parse(s2); \/\/&#8211; FormatException<br \/>\nresult = Int32.Parse(s3); \/\/&#8211; ArgumentNullException<br \/>\nresult = Int32.Parse(s4); \/\/&#8211; OverflowException<\/p>\n<p>3) Convert accepts an object as its paramerter &#8212;- One major difference is that Convert does not throw a ArgumentNullException while Parse does. Your cast would also throw an exception if it null. You can get around that by using (int?)Something;<\/p>\n<p>result = Convert.ToInt32(s1); \/\/&#8211; 1234<br \/>\nresult = Convert.ToInt32(s2); \/\/&#8211; FormatException<br \/>\nresult = Convert.ToInt32(s3); \/\/&#8211; 0<br \/>\nresult = Convert.ToInt32(s4); \/\/&#8211; OverflowException<\/p>\n<p>4)Int32.TryParse(string, out int)<\/p>\n<p><code>Int32.Parse(string, out int)<\/code> method converts the specified <code>string <\/code>representation of 32-bit signed integer equivalent to <code>out <\/code>variable, and returns <code>true <\/code>if it is parsed successfully, <code>false <\/code>otherwise. This method is available in C# 2.0. When <code>s <\/code>is a <code>null <\/code>reference, it will return <code>0 <\/code>rather than throw <code>ArgumentNullException<\/code>. If <code>s <\/code>is other than an <code>integer <\/code>value, the <code>out <\/code>variable will have <code>0 <\/code>rather than <code>FormatException<\/code>. When <code>s <\/code>represents a number less than <code>MinValue <\/code>or greater than <code>MaxValue<\/code>, the <code>out <\/code>variable will have <code>0 <\/code>rather than <code>OverflowException<\/code>. For example:<\/p>\n<div id=\"premain2\"><\/div>\n<pre id=\"pre2\"> success = Int32.TryParse(s1, out result); \/\/-- success =&gt; true; result =&gt; 1234 \n success = Int32.TryParse(s2, out result); \/\/-- success =&gt; false; result =&gt; 0 \n success = Int32.TryParse(s3, out result); \/\/-- success =&gt; false; result =&gt; 0 \n success = Int32.TryParse(s4, out result); \/\/-- success =&gt; false; result =&gt; 0<\/pre>\n<p><code>Convert.ToInt32<\/code> is better than <code>Int32.Parse<\/code> since it returns <code>0 <\/code>rather than an exception. But again, according to the requirement, this can be used. <code>TryParse <\/code>will be the best since it always handles exceptions by\u00a0itself.<\/p>\n<p>One major difference is that <code>Convert<\/code> does not throw a <code>ArgumentNullException<\/code> while <code>Parse<\/code> does. Your cast would also throw an exception if it null. You can get around that by using<\/p>\n<pre><code>(int?)Something;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">(int)Something; int.Parse(Something); Convert.ToInt32(Something); 1) that is a cast 2) Parsing taking in a string and attempts to convert it to a type. &#8212;when string is null refence,it will throw ArgumentNullException.if string is other than integer value ,it will throw FormatException.if reperesents a number less than Min or maxvalue than will throw overflowexception. string s1 = &#8220;1234&#8221;; string s2 = &#8220;1234.65&#8221;;&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=136\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-136","post","type-post","status-publish","format-standard","hentry","category-c-vb","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=136"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/136\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}