本文整理汇总了C#中System.ValueType类的典型用法代码示例。如果您正苦于以下问题:C# ValueType类的具体用法?C# ValueType怎么用?C# ValueType使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ValueType类属于System命名空间,在下文中一共展示了ValueType类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compare
//引入命名空间
using System;
using System.Numerics;
public class Utility
{
public enum NumericRelationship {
GreaterThan = 1,
EqualTo = 0,
LessThan = -1
};
public static NumericRelationship Compare(ValueType value1, ValueType value2)
{
if (! IsNumeric(value1))
throw new ArgumentException("value1 is not a number.");
else if (! IsNumeric(value2))
throw new ArgumentException("value2 is not a number.");
// Use BigInteger as common integral type
if (IsInteger(value1) && IsInteger(value2)) {
BigInteger bigint1 = (BigInteger) value1;
BigInteger bigint2 = (BigInteger) value2;
return (NumericRelationship) BigInteger.Compare(bigint1, bigint2);
}
// At least one value is floating point; use Double.
else {
Double dbl1 = 0;
Double dbl2 = 0;
try {
dbl1 = Convert.ToDouble(value1);
}
catch (OverflowException) {
Console.WriteLine("value1 is outside the range of a Double.");
}
try {
dbl2 = Convert.ToDouble(value2);
}
catch (OverflowException) {
Console.WriteLine("value2 is outside the range of a Double.");
}
return (NumericRelationship) dbl1.CompareTo(dbl2);
}
}
public static bool IsInteger(ValueType value)
{
return (value is SByte || value is Int16 || value is Int32
|| value is Int64 || value is Byte || value is UInt16
|| value is UInt32 || value is UInt64
|| value is BigInteger);
}
public static bool IsFloat(ValueType value)
{
return (value is float | value is double | value is Decimal);
}
public static bool IsNumeric(ValueType value)
{
return (value is Byte ||
value is Int16 ||
value is Int32 ||
value is Int64 ||
value is SByte ||
value is UInt16 ||
value is UInt32 ||
value is UInt64 ||
value is BigInteger ||
value is Decimal ||
value is Double ||
value is Single);
}
}
示例2: Main
public class Example
{
public static void Main()
{
Console.WriteLine(Utility.IsNumeric(12));
Console.WriteLine(Utility.IsNumeric(true));
Console.WriteLine(Utility.IsNumeric('c'));
Console.WriteLine(Utility.IsNumeric(new DateTime(2012, 1, 1)));
Console.WriteLine(Utility.IsInteger(12.2));
Console.WriteLine(Utility.IsInteger(123456789));
Console.WriteLine(Utility.IsFloat(true));
Console.WriteLine(Utility.IsFloat(12.2));
Console.WriteLine(Utility.IsFloat(12));
Console.WriteLine("{0} {1} {2}", 12.1, Utility.Compare(12.1, 12), 12);
}
}
输出:
True False False False False True False True False 12.1 GreaterThan 12