繁体   English   中英

如何在C#中初始化BigInteger?

[英]How to initialize a BigInteger in C#?

有人可以告诉我如何使用System.Numerics.BigInteger数据类型? 我尝试使用它作为参考 - http://msdn.microsoft.com/en-us/library/system.numerics.biginteger%28VS.100%29.aspx

但是我的计算机上没有System.Numerics命名空间。 我已经安装了VS2010 Ultimate RC,我有.NET Framework 4.0。 有人可以指导我完成这个吗?

它应该在那里,你记得添加一个参考?

右键单击项目,单击“添加引用”,然后在最左侧的选项卡中,选择“System.Numerics”

然后你可以添加它并使用它。

您的项目定位的是什么版本的.NET 4? 确保它的目标是整个框架而不是客户端配置文件。 我刚刚确认System.Numerics.dll是.NET 4 Client Profile的一部分,所以这应该不是问题。

完成此操作后,请确保您在项目中引用了System.Numerics.dll

你的项目参考中有System.Numerics.dll吗?

确保包含对System.Numerics 的引用否则您将看不到命名空间。 MSDN文档是一个很好的资源,可以查看您必须引用哪些程序集来获取哪些名称空间。

您需要自己添加参考System.Numerics如其他答案所述,如果您安装了.NET 4System.Numerics.dll应该在那里。

然后,如果你的价值真的很大,你会尝试:

var myBigInteger = new BigInteger(50000000000000000000000000000000000000000000);

您将收到编译错误:

编译常量值不正确

最简单的方法是使用文本文字:

var myBigInteger = BigInteger.Parse("50000000000000000000000000000000000000000000");

如果在Visual Studio 2010中,则需要手动将程序集引用添加到项目中。 您可以通过添加引用> .NET并向下滚动直到找到System.Numerics(System.Numerics.dll)版本4.0.0.0并选择它。

完成后,您需要在代码中添加using语句:

using System.Numerics;

然后初始化BigInteger有几种方法:

您可以执行以下操作:

BigInteger x = new BigInteger();
x = BigInteger.Zero; // initializes x to 0
x = BigInteger.One; // initializes x to 1

或者您可以使用带有整数文字的构造函数

BigInteger x = new BigInteger();
x = BigInteger(0); // initializes x to 0
x = BigInteger(1); // initializes x to 1

或者更容易

BigInteger x;
x = 0; // initializes x to 0
x = 1; // initializes x to 1

最后一种方法在C#和VB.NET中运行良好,但是,还有其他语言不支持使用文字初始化BigInteger,例如:JScript.NET,Phalanger。

先决条件:.NET Framework 4

脚步:

  1. 添加参考System.Numerics.dll
  2. 在您的课程中添加Imports System.Numerics

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM