本文整理汇总了C#中System.Byte.TryParse方法的典型用法代码示例。如果您正苦于以下问题:C# Byte.TryParse方法的具体用法?C# Byte.TryParse怎么用?C# Byte.TryParse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Byte
的用法示例。
在下文中一共展示了Byte.TryParse方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
public class ByteConversion
{
public static void Main()
{
string[] byteStrings = { null, string.Empty, "1024",
"100.1", "100", "+100", "-100",
"000000000000000100", "00,100",
" 20 ", "FF", "0x1F" };
foreach (var byteString in byteStrings)
{
CallTryParse(byteString);
}
}
private static void CallTryParse(string stringToConvert)
{
byte byteValue;
bool success = Byte.TryParse(stringToConvert, out byteValue);
if (success)
{
Console.WriteLine("Converted '{0}' to {1}",
stringToConvert, byteValue);
}
else
{
Console.WriteLine("Attempted conversion of '{0}' failed.",
stringToConvert);
}
}
}
输出:
Attempted conversion of '' failed. Attempted conversion of '' failed. Attempted conversion of '1024' failed. Attempted conversion of '100.1' failed. Converted '100' to 100 Converted '+100' to 100 Attempted conversion of '-100' failed. Converted '000000000000000100' to 100 Attempted conversion of '00,100' failed. Converted ' 20 ' to 20 Attempted conversion of 'FF' failed. Attempted conversion of '0x1F' failed.
示例2: Main
//引入命名空间
using System;
using System.Globalization;
public class ByteConversion2
{
public static void Main()
{
string byteString;
NumberStyles styles;
byteString = "1024";
styles = NumberStyles.Integer;
CallTryParse(byteString, styles);
byteString = "100.1";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(byteString, styles);
byteString = "100.0";
CallTryParse(byteString, styles);
byteString = "+100";
styles = NumberStyles.Integer | NumberStyles.AllowLeadingSign
| NumberStyles.AllowTrailingSign;
CallTryParse(byteString, styles);
byteString = "-100";
CallTryParse(byteString, styles);
byteString = "000000000000000100";
CallTryParse(byteString, styles);
byteString = "00,100";
styles = NumberStyles.Integer | NumberStyles.AllowThousands;
CallTryParse(byteString, styles);
byteString = "2E+3 ";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(byteString, styles);
byteString = "FF";
styles = NumberStyles.HexNumber;
CallTryParse(byteString, styles);
byteString = "0x1F";
CallTryParse(byteString, styles);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
Byte byteValue;
bool result = Byte.TryParse(stringToConvert, styles,
null as IFormatProvider, out byteValue);
if (result)
Console.WriteLine("Converted '{0}' to {1}",
stringToConvert, byteValue);
else
Console.WriteLine("Attempted conversion of '{0}' failed.",
stringToConvert.ToString());
}
}
输出:
Attempted conversion of '1024' failed. Attempted conversion of '100.1' failed. Converted '100.0' to 100 Converted '+100' to 100 Attempted conversion of '-100' failed. Converted '000000000000000100' to 100 Converted '00,100' to 100 Attempted conversion of '2E+3 ' failed. Converted 'FF' to 255 Attempted conversion of '0x1F' failed.