C# 我引用的是一个常量类静态变量,但当我引用时,值会发生某种变化

C# 我引用的是一个常量类静态变量,但当我引用时,值会发生某种变化,c#,visual-studio-2013,reference,static,C#,Visual Studio 2013,Reference,Static,这是我引用的常量类。 下面是试图使用引用的类 这是我收到的错误消息 ConstantClass的类型初始值设定项引发了一个异常--->System.OverflowException:值对于十进制而言太大或太小 有什么想法吗 using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; using System.Linq; using System.Text;

这是我引用的常量类。 下面是试图使用引用的类

这是我收到的错误消息

ConstantClass的类型初始值设定项引发了一个异常--->System.OverflowException:值对于十进制而言太大或太小

有什么想法吗

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnitTests
{
    [TestClass]

    public static class ConstantClass
    {
        public static int positiveInt = 10;
        public static int negativeInt = -10;
        public static long positivePastLimitInt = unchecked((int)2147483648);
        public static long negativePastLimitInt = unchecked((int)-2147483649);
        public static int zeroInt = 0;


        public static char lowerChar = 'c';
        public static char spaceChar = ' ';
        public static char symbolChar = '@';
        public static char numberChar = '1';
        public static char upperChar = 'D';

        public static string lowerString = "hello";
        public static string upperString = "HELLO";
        public static string emptyString = "";
        public static string spaceString = " ";
        public static string tabString = "    ";
        public static string symbolString = "!^&";
        public static string nullString = null;

        public static short positiveShort = 10;
        public static short negativeShort = -10;
        public static short zeroShort = 0;
        public static int positivePastLimitShort = unchecked((short)32768);
        public static int negativePastLimitShort = unchecked((short)-32769);

        public static long positiveLong = 10;
        public static long negativeLong = -10;
        public static long zeroLong = 0;
        public static Int64 positivePastLimitLong = (long)2147483648;
        public static Int64 negativePastLimitLong = (long)-2147483649;

        public static double positiveDouble = 10.0;
        public static double negativeDouble = -10.0;
        public static double zeroDouble = 0.0;
        public static double positiveLimitDouble = double.MaxValue;
        public static double negativeLimitDouble = double.MinValue;

        public static float positiveFloat = 10.0F;
        public static float negativeFloat = -10.0F;
        public static float zeroFloat = 0.0F;
        public static double positivePastLimitFloat = (float)float.MaxValue + 1;
        public static double negativePastLimitFloat = (float)float.MinValue - 1;

        public static bool positiveBool = true;
        public static bool negativeBool = false;


        //Here is the variable im trying to use.
        public static decimal positiveDecimal = 10.0m;
        public static decimal negativeDecimal = -10.0m;
        public static decimal zeroDecimal = 0.0m;
        public static decimal positivePastLimitDecimal = Convert.ToDecimal(80000000000000E+40);
        public static decimal negativePastLimitDecimal = Convert.ToDecimal(-8000000000000E-40);
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTests
{
    [TestClass]
    public class ByteDecimalTests
    {
        /// <summary>
        /// 
        /// </summary>
        [TestMethod]
        public void DecimalToBytes_WhenDecimalIsPositive()
        {
            //This is where i reference the constant class
            Decimal positiveDecimal = ConstantClass.positiveDecimal;
            //Decimal positiveDecimal = 12.98m;
            String positiveDecimalString = positiveDecimal.ToString();

            byte[] positiveDecimalArray = Encoding.ASCII.GetBytes(positiveDecimalString);
            byte[] array = ByteDecimal.DecimalToBytes(positiveDecimal);

            System.Diagnostics.Debug.WriteLine(Encoding.Default.GetString(positiveDecimalArray));
            System.Diagnostics.Debug.WriteLine(Encoding.Default.GetString(array));
            Assert.AreEqual(array, positiveDecimalArray);

        }
   }
}
使用Microsoft.VisualStudio.TestTools.UnitTesting;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间单元测试
{
[测试类]
公共静态类ConstantClass
{
公共静态整数正整数=10;
公共静态int negativeInt=-10;
public static long positivePostLimitint=unchecked((int)2147483648);
public static long negativePostLimitint=unchecked((int)-2147483649);
公共静态int zeroInt=0;
publicstaticcharlowerchar='c';
公共静态字符spaceChar='';
公共静态字符symbolChar='@';
publicstaticcharnumberchar='1';
公共静态字符upperChar='D';
公共静态字符串lowerString=“hello”;
公共静态字符串upperString=“HELLO”;
公共静态字符串emptyString=“”;
公共静态字符串spaceString=“”;
公共静态字符串tabString=“”;
公共静态字符串symbolString=“!^&”;
公共静态字符串nullString=null;
公共静态短正短路=10;
公共静态短负运动=-10;
公共静态短零短=0;
public static int positivePostLimitShort=未选中((短)32768);
公共静态int negativePostLimitShort=未选中((短)-32769);
公共静态长正长=10;
公共静态长负长=-10;
公共静态长零长=0;
公共静态Int64 PositivePostLimitLong=(长)2147483648;
公共静态Int64 NegativePostLimitLong=(长)-2147483649;
公共静态双正双倍=10.0;
公共静态双负双倍=-10.0;
公共静态双零双=0.0;
public static double positiveLimitDouble=double.MaxValue;
public static double negativeLimitDouble=double.MinValue;
公共静态浮点正浮点=10.0F;
公共静态浮点negativeFloat=-10.0F;
公共静态浮点零浮点=0.0F;
public static double positivePostLimitFloat=(float)float.MaxValue+1;
public static double negativePostLimitFloat=(float)float.MinValue-1;
公共静态bool positiveBool=true;
公共静态bool negativeBool=false;
//这是我试图使用的变量。
公共静态十进制正小数=10.0m;
公共静态小数负小数=-10.0m;
公共静态十进制零十进制=0.0m;
public static decimal positivePostLimitDecimal=Convert.ToDecimal(800000000000E+40);
public static decimal negativePostLimitDecimal=Convert.ToDecimal(-80000000000E-40);
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.VisualStudio.TestTools.UnitTesting;
命名空间单元测试
{
[测试类]
公共类ByteDecimalTests
{
/// 
/// 
/// 
[测试方法]
当CIMALISION为正值时的公共空分贝数()
{
//这就是我引用常量类的地方
十进制正小数=常量类正小数;
//十进制正整数=12.98m;
字符串positiveCimalString=positiveCimal.ToString();
byte[]positivedCimalArray=Encoding.ASCII.GetBytes(positivedCimalString);
字节[]数组=字节小数.DecimalToBytes(正整数);
System.Diagnostics.Debug.WriteLine(Encoding.Default.GetString(positiveDecimalArray));
System.Diagnostics.Debug.WriteLine(Encoding.Default.GetString(数组));
AreEqual(数组,正定数组);
}
}
}
您的问题在于:

 public static decimal positivePastLimitDecimal = Convert.ToDecimal(80000000000000E+40)
 public static decimal negativePastLimitDecimal = Convert.ToDecimal(-8000000000000E-40);
你不能把这么大的数字存储在小数点上。使用float或double


您可以存储在十进制中的值是:79228162514264337593543950335。

也许这有助于您了解自己在做什么,从而决定如何解决它。我将把这个简短的例子作为更容易研究的例子,但是其他的例子也可以这样说

从您的代码: public static int positivePostLimitShort=未选中((短)32768)

短路在内部存储为16位值,15位表示数据,1位表示符号

标志b14 b13 b12 b11 b10 b09 b08 b07 b06 b05 b04 b03 b02 b01 b00

当您尝试使用需要超过15位来表示的值时,将发生溢出。 二进制形式的32768是10000000000000 但您不能使用位15,它是为登录短片保留的(在ushort中,您可以使用所有16位,因为没有为登录短片保留任何位)

你甚至可以压制演员阵容和未经检查的角色,只需执行以下操作:

public static int positivePastLimitShort = 32768;
因为int在内部使用32位,1表示符号,31表示数据

这是有效的,因为您有一个可以存储更大数字的值类型


因此,使用int表示通过限制的短路,使用long表示通过限制的int,等等。问题是要处理超过较大容量类型限制的数字,如长、双精度或十进制。对于这些情况,您需要一种不同的方法。

我需要一个大于十进制的值,但可以转换为十进制。我一直无法找到如何做到这一点。我需要测试溢出。有什么想法吗?谢谢您将需要寻找一些外部解决方案,也许这对您有用:我不想为了这个小问题导入所有代码。我很感激