{"id":11855,"date":"2016-10-26T00:00:00","date_gmt":"2016-10-25T16:00:00","guid":{"rendered":"https:\/\/fgchen.com\/wpedu2\/2016\/10\/26\/%e3%80%90c-%e5%b0%87%e5%ad%97%e4%b8%b2%e8%bd%89%e6%8f%9b%e7%82%ba%e6%95%b8%e5%80%bc%e3%80%91parse-convert-tryparse\/"},"modified":"2026-03-30T14:55:55","modified_gmt":"2026-03-30T06:55:55","slug":"%e3%80%90c-%e5%b0%87%e5%ad%97%e4%b8%b2%e8%bd%89%e6%8f%9b%e7%82%ba%e6%95%b8%e5%80%bc%e3%80%91parse-convert-tryparse","status":"publish","type":"post","link":"https:\/\/fgchen.com\/wpedu\/2016\/10\/%e3%80%90c-%e5%b0%87%e5%ad%97%e4%b8%b2%e8%bd%89%e6%8f%9b%e7%82%ba%e6%95%b8%e5%80%bc%e3%80%91parse-convert-tryparse\/","title":{"rendered":"\u3010\u7a0b\u5f0f\u8a2d\u8a08-C#\u3011\u5c07\u5b57\u4e32\u8f49\u63db\u70ba\u6578\u503c Parse (Convert) &amp; TryParse"},"content":{"rendered":"\u5f9e\u8996\u7a97\u6587\u5b57\u65b9\u584a\u53d6\u5f97\u7684\u8f38\u5165\uff0c\u96d6\u7136&#8221;\u770b&#8221;\u8d77\u4f86\u662f\u6578\u503c\uff0c\u672c\u8cea\u662f\u5b57\u4e32\uff0c\u9700\u8981\u9032\u4e00\u6b65\u5730\u5c07\u8f38\u5165\u7684\u5b57\u4e32\u8f49\u63db\u6210\u5c0d\u61c9\u7684\u6578\u503c\u578b\u614b\uff0c\u624d\u80fd\u9032\u884c\u6578\u503c\u578b\u614b\u7684\u904b\u7b97(\u4f8b\uff1a\u52a0\u6e1b\u4e58\u9664\u7b49\u2026)\n&nbsp;\nParse\u65b9\u6cd5\uff1a\n\u5728C#\u4e2d\uff0c\u6bcf\u500b\u57fa\u672c\u8cc7\u6599\u578b\u614b(char, byte, short, int, long, float, double\u7b49)\u90fd\u6703\u5c0d\u61c9\u4e00\u500b\u985e\u5225\u578b\u614b(Char, Byte, Short, Int32, Long, Float, Double\u7b49)\uff0c\u63d0\u4f9b\u8a72\u6578\u503c\u578b\u614b\u7684\u65b9\u6cd5\uff0c\u5176\u4e2dParse\u65b9\u6cd5\u662f\u5c07\u50b3\u5165\u7684\u5b57\u4e32\u8f49\u6210\u5c0d\u61c9\u7684\u6578\u503c\uff0cParse\u6216Convert\u65b9\u6cd5\u5728\u8f49\u63db\u5b57\u4e32\u6642\uff0c\u767c\u751f\u554f\u984c\u6703\u7522\u751f\u4f8b\u5916\uff0c\u4f8b\u5982\u6709\u5c0f\u6578\u9ede\u7684\u5b57\u4e32\u6578\u503c\u8f49\u63db\u6210\u6574\u6578\u5c31\u6703\u767c\u751f\u683c\u5f0f\u932f\u8aa4\u7684\u4f8b\u5916\uff0c\u6b64\u6642\uff0c\u4f60\u6700\u597d\u5beb\u4e00\u500bcatch\u88dc\u6349\u4f8b\u5916\u7684\u5340\u584a\uff0c\u5c0d\u7522\u751f\u7684\u4f8b\u5916\u52a0\u4ee5\u8655\u7406\u3002\nTryParse\u57f7\u884c\u548cParse\u4e00\u6a23\u7684\u529f\u80fd\uff0c\u4e0d\u904eTryParse\u767c\u751f\u932f\u8aa4\u6642\u4e0d\u6703\u62cb\u51fa\u4f8b\u5916\uff0c\u56de\u50b3\u4e00\u500b\u5e03\u6797\u503c\uff0c\u6210\u529f\u56de\u50b3true\uff0c\u8f49\u578b\u5931\u6557\u6703\u8f49\u6210\u521d\u59cb\u503c\uff0c\u4e26\u56de\u50b3false\u3002\nParse\u4f8b\uff1a\n\n<pre class=\"lang:c# decode:true\">int numVal = Int32.Parse(\"-105\");\nConsole.WriteLine(numVal);\n\/\/ \u8f38\u51fa: -105<\/pre>\n\nTryParse\u4f8b\uff1a\n\n<pre class=\"lang:c# decode:true\">\/\/ TryParse \u56de\u50b3 true\uff0c\u5982\u679c\u8f49\u63db\u662f\u6210\u529f\u7684\u8a71\n\/\/ \u4e26\u4e14\u5c07\u7d50\u679c\u5132\u5b58\u5230 j \u8b8a\u6578\nint j;\nif (Int32.TryParse(\"-105\", out j))\n    Console.WriteLine(j);\nelse\n    Console.WriteLine(\"\u5b57\u4e32\u7121\u6cd5\u89e3\u6790\");\n\/\/ \u8f38\u51fa: -105<\/pre>\n\nPasre with Try-catch\n\n<pre class=\"lang:c# decode:true \">try\n{\n    int m = Int32.Parse(\"abc\");\n}\ncatch (FormatException e)\n{\n    Console.WriteLine(e.Message);\n}\n\/\/ \u8f38\u51fa: Input string was not in a correct format.<\/pre>\n\nTryParse\u4f8b\uff1a\n\n<pre class=\"lang:c# decode:true\">string inputString = \"abc\";\nint numValue;\nbool parsed = Int32.TryParse(inputString, out numValue);\nif (!parsed)\n    Console.WriteLine(\"Int32.TryParse \u7121\u6cd5\u8f49\u63db '{0}' \u6210\u70ba\u4e00\u500b int.n\", inputString);\n\/\/ \u8f38\u51fa: Int32.TryParse \u7121\u6cd5\u8f49\u63db 'abc' \u6210\u70ba\u4e00\u500b int.<\/pre>\n\n&nbsp;\n\n<h3>\u8cc7\u672c\u8cc7\u6599\u578b\u614b\uff1a<\/h3>\n\n&lt;\n\ndiv id=&#8221;scid:812469c5-0cb0-4c63-8c15-c81123a09de7:9f284f2b-369c-4e52-9f90-9cb0e4df8f68&#8243; class=&#8221;wlWriterEditableSmartContent&#8221;&gt;\u00a0<div class=\"su-table su-table-alternate\"><\/div>\n<div class=\"wlWriterEditableSmartContent\"><\/div>\n<div class=\"wlWriterEditableSmartContent\">\n<table style=\"width: 805px\" summary=\"table\">\n<tbody>\n<tr style=\"height: 24px\">\n<th style=\"width: 91px;height: 24px\" scope=\"col\">\u6578\u503c\u578b\u614b<\/th>\n<th style=\"width: 93px;height: 24px\" scope=\"col\">\u6578\u503c\u985e\u5225<\/th>\n<th style=\"width: 195px;height: 24px\" scope=\"col\">\u578b\u614b<\/th>\n<th style=\"width: 76px;height: 24px\" scope=\"col\">\u5bec\u5ea6<\/th>\n<th style=\"width: 336px;height: 24px\" scope=\"col\">\u6578\u503c\u7bc4\u570d(bits)<\/th>\n<\/tr>\n<tr style=\"height: 24px\">\n<td style=\"width: 91px;height: 24px\" data-th=\"Short Name\"><span class=\"input\">byte<\/span><\/td>\n<td style=\"width: 93px;height: 24px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.byte(v=vs.90).aspx\">Byte<\/a><\/td>\n<td style=\"width: 195px;height: 24px\" data-th=\"Type\">Unsigned integer<\/td>\n<td style=\"width: 76px;height: 24px\" data-th=\"Width\">8<\/td>\n<td style=\"width: 336px;height: 24px\" data-th=\"Range (bits)\">0 to 255<\/td>\n<\/tr>\n<tr style=\"height: 24px\">\n<td style=\"width: 91px;height: 24px\" data-th=\"Short Name\"><span class=\"input\">sbyte<\/span><\/td>\n<td style=\"width: 93px;height: 24px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.sbyte(v=vs.90).aspx\">SByte<\/a><\/td>\n<td style=\"width: 195px;height: 24px\" data-th=\"Type\">Signed integer<\/td>\n<td style=\"width: 76px;height: 24px\" data-th=\"Width\">8<\/td>\n<td style=\"width: 336px;height: 24px\" data-th=\"Range (bits)\">-128 to 127<\/td>\n<\/tr>\n<tr style=\"height: 48px\">\n<td style=\"width: 91px;height: 48px\" data-th=\"Short Name\"><span class=\"input\">int<\/span><\/td>\n<td style=\"width: 93px;height: 48px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.int32(v=vs.90).aspx\">Int32<\/a><\/td>\n<td style=\"width: 195px;height: 48px\" data-th=\"Type\">Signed integer<\/td>\n<td style=\"width: 76px;height: 48px\" data-th=\"Width\">32<\/td>\n<td style=\"width: 336px;height: 48px\" data-th=\"Range (bits)\">-2,147,483,648 to 2,147,483,647<\/td>\n<\/tr>\n<tr style=\"height: 24px\">\n<td style=\"width: 91px;height: 24px\" data-th=\"Short Name\"><span class=\"input\">uint<\/span><\/td>\n<td style=\"width: 93px;height: 24px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.uint32(v=vs.90).aspx\">UInt32<\/a><\/td>\n<td style=\"width: 195px;height: 24px\" data-th=\"Type\">Unsigned integer<\/td>\n<td style=\"width: 76px;height: 24px\" data-th=\"Width\">32<\/td>\n<td style=\"width: 336px;height: 24px\" data-th=\"Range (bits)\">0 to 4294967295<\/td>\n<\/tr>\n<tr style=\"height: 24px\">\n<td style=\"width: 91px;height: 24px\" data-th=\"Short Name\"><span class=\"input\">short<\/span><\/td>\n<td style=\"width: 93px;height: 24px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.int16(v=vs.90).aspx\">Int16<\/a><\/td>\n<td style=\"width: 195px;height: 24px\" data-th=\"Type\">Signed integer<\/td>\n<td style=\"width: 76px;height: 24px\" data-th=\"Width\">16<\/td>\n<td style=\"width: 336px;height: 24px\" data-th=\"Range (bits)\">-32,768 to 32,767<\/td>\n<\/tr>\n<tr style=\"height: 24px\">\n<td style=\"width: 91px;height: 24px\" data-th=\"Short Name\"><span class=\"input\">ushort<\/span><\/td>\n<td style=\"width: 93px;height: 24px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.uint16(v=vs.90).aspx\">UInt16<\/a><\/td>\n<td style=\"width: 195px;height: 24px\" data-th=\"Type\">Unsigned integer<\/td>\n<td style=\"width: 76px;height: 24px\" data-th=\"Width\">16<\/td>\n<td style=\"width: 336px;height: 24px\" data-th=\"Range (bits)\">0 to 65535<\/td>\n<\/tr>\n<tr style=\"height: 48px\">\n<td style=\"width: 91px;height: 48px\" data-th=\"Short Name\"><span class=\"input\">long<\/span><\/td>\n<td style=\"width: 93px;height: 48px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.int64(v=vs.90).aspx\">Int64<\/a><\/td>\n<td style=\"width: 195px;height: 48px\" data-th=\"Type\">Signed integer<\/td>\n<td style=\"width: 76px;height: 48px\" data-th=\"Width\">64<\/td>\n<td style=\"width: 336px;height: 48px\" data-th=\"Range (bits)\">-9223372036854775808 to 9223372036854775807<\/td>\n<\/tr>\n<tr style=\"height: 24px\">\n<td style=\"width: 91px;height: 24px\" data-th=\"Short Name\"><span class=\"input\">ulong<\/span><\/td>\n<td style=\"width: 93px;height: 24px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.uint64(v=vs.90).aspx\">UInt64<\/a><\/td>\n<td style=\"width: 195px;height: 24px\" data-th=\"Type\">Unsigned integer<\/td>\n<td style=\"width: 76px;height: 24px\" data-th=\"Width\">64<\/td>\n<td style=\"width: 336px;height: 24px\" data-th=\"Range (bits)\">0 to 18446744073709551615<\/td>\n<\/tr>\n<tr style=\"height: 48px\">\n<td style=\"width: 91px;height: 48px\" data-th=\"Short Name\"><span class=\"input\">float<\/span><\/td>\n<td style=\"width: 93px;height: 48px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.single(v=vs.90).aspx\">Single<\/a><\/td>\n<td style=\"width: 195px;height: 48px\" data-th=\"Type\">Single-precision floating point type<\/td>\n<td style=\"width: 76px;height: 48px\" data-th=\"Width\">32<\/td>\n<td style=\"width: 336px;height: 48px\" data-th=\"Range (bits)\">-3.402823e38 to 3.402823e38<\/td>\n<\/tr>\n<tr style=\"height: 48px\">\n<td style=\"width: 91px;height: 48px\" data-th=\"Short Name\"><span class=\"input\">double<\/span><\/td>\n<td style=\"width: 93px;height: 48px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.double(v=vs.90).aspx\">Double<\/a><\/td>\n<td style=\"width: 195px;height: 48px\" data-th=\"Type\">Double-precision floating point type<\/td>\n<td style=\"width: 76px;height: 48px\" data-th=\"Width\">64<\/td>\n<td style=\"width: 336px;height: 48px\" data-th=\"Range (bits)\">-1.79769313486232e308 to 1.79769313486232e308<\/td>\n<\/tr>\n<tr style=\"height: 24px\">\n<td style=\"width: 91px;height: 24px\" data-th=\"Short Name\"><span class=\"input\">char<\/span><\/td>\n<td style=\"width: 93px;height: 24px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.char(v=vs.90).aspx\">Char<\/a><\/td>\n<td style=\"width: 195px;height: 24px\" data-th=\"Type\">A single Unicode character<\/td>\n<td style=\"width: 76px;height: 24px\" data-th=\"Width\">16<\/td>\n<td style=\"width: 336px;height: 24px\" data-th=\"Range (bits)\">Unicode symbols used in text<\/td>\n<\/tr>\n<tr style=\"height: 24px\">\n<td style=\"width: 91px;height: 24px\" data-th=\"Short Name\"><span class=\"input\">bool<\/span><\/td>\n<td style=\"width: 93px;height: 24px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.boolean(v=vs.90).aspx\">Boolean<\/a><\/td>\n<td style=\"width: 195px;height: 24px\" data-th=\"Type\">Logical Boolean type<\/td>\n<td style=\"width: 76px;height: 24px\" data-th=\"Width\">8<\/td>\n<td style=\"width: 336px;height: 24px\" data-th=\"Range (bits)\">True or false<\/td>\n<\/tr>\n<tr style=\"height: 24px\">\n<td style=\"width: 91px;height: 24px\" data-th=\"Short Name\"><span class=\"input\">object<\/span><\/td>\n<td style=\"width: 93px;height: 24px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.object(v=vs.90).aspx\">Object<\/a><\/td>\n<td style=\"width: 195px;height: 24px\" data-th=\"Type\">Base type of all other types<\/td>\n<td style=\"width: 76px;height: 24px\" data-th=\"Width\"><\/td>\n<td style=\"width: 336px;height: 24px\" data-th=\"Range (bits)\"><\/td>\n<\/tr>\n<tr style=\"height: 24px\">\n<td style=\"width: 91px;height: 24px\" data-th=\"Short Name\"><span class=\"input\">string<\/span><\/td>\n<td style=\"width: 93px;height: 24px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.string(v=vs.90).aspx\">String<\/a><\/td>\n<td style=\"width: 195px;height: 24px\" data-th=\"Type\">A sequence of characters<\/td>\n<td style=\"width: 76px;height: 24px\" data-th=\"Width\"><\/td>\n<td style=\"width: 336px;height: 24px\" data-th=\"Range (bits)\"><\/td>\n<\/tr>\n<tr style=\"height: 72px\">\n<td style=\"width: 91px;height: 72px\" data-th=\"Short Name\"><span class=\"input\">decimal<\/span><\/td>\n<td style=\"width: 93px;height: 72px\" data-th=\".NET Class\"><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.decimal(v=vs.90).aspx\">Decimal<\/a><\/td>\n<td style=\"width: 195px;height: 72px\" data-th=\"Type\">Precise fractional or integral type that can represent decimal numbers with 29 significant digits<\/td>\n<td style=\"width: 76px;height: 72px\" data-th=\"Width\">128<\/td>\n<td style=\"width: 336px;height: 72px\" data-th=\"Range (bits)\">\u00b11.0\u00a0\u00d7\u00a010e\u221228 to \u00b17.9\u00a0\u00d7\u00a010e28<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>\u5f9e\u8996\u7a97\u6587\u5b57\u65b9\u584a\u53d6\u5f97\u7684\u8f38\u5165\uff0c\u96d6\u7136&#8221;\u770b&#8221;\u8d77\u4f86\u662f\u6578\u503c\uff0c\u672c\u8cea\u662f\u5b57 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[266],"tags":[],"class_list":["post-11855","post","type-post","status-publish","format-standard","hentry","category-266"],"_links":{"self":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/11855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/comments?post=11855"}],"version-history":[{"count":1,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/11855\/revisions"}],"predecessor-version":[{"id":13555,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/11855\/revisions\/13555"}],"wp:attachment":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/media?parent=11855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/categories?post=11855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/tags?post=11855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}