BigInteger是CTS支持的类型(.NET)吗?

时间:2017-05-31 14:47:30

标签: c# .net

我正在阅读BigInteger s(C#中的数字类型“向上扩展”。例如,当执行斐波纳契数列时,数字很快就会变得过大long。)

现在,我想知道CTS是否支持BigInteger。例如,我可以在所有 .NET语言中使用BigInteger类型吗?

3 个答案:

答案 0 :(得分:6)

为什么不进行实验并查看?

  uisng System.Linq;
  using System.Numerics;
  uisng System.Reflection;
  ...

  var result = typeof(BigInteger)
    .Assembly
    .GetCustomAttributes()
    .Select(attr => attr.GetType().Name)
    .ToArray();

  Console.Write(string.Join(Environment.NewLine, result));

结果:

AssemblySignatureKeyAttribute
DebuggableAttribute
ComVisibleAttribute
CLSCompliantAttribute                    <--------------- that is!
AllowPartiallyTrustedCallersAttribute
SecurityRulesAttribute
AssemblyTitleAttribute
AssemblyDescriptionAttribute
AssemblyDefaultAliasAttribute
AssemblyCompanyAttribute
AssemblyProductAttribute
AssemblyCopyrightAttribute
AssemblyFileVersionAttribute
AssemblyInformationalVersionAttribute
SatelliteContractVersionAttribute
NeutralResourcesLanguageAttribute
AssemblyDelaySignAttribute
AssemblyKeyFileAttribute
CompilationRelaxationsAttribute
RuntimeCompatibilityAttribute
SecurityPermissionAttribute

如您所见,BigInteger struct在程序集中实现,该程序集明确声明CLSCompliantAttribute。答案是&#34;是的,BigInteger符合 CLS ,可以在任何.Net语言中使用&#34;。

https://msdn.microsoft.com/en-us/library/system.clscompliantattribute(v=vs.110).aspx

了解详情

修改:但是,某些BigInteger成员 CLSCompliant(请参阅Tim Schmelter的评论):

 var result = typeof(BigInteger)
    .GetMembers(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)
    .Where(member => member
       .GetCustomAttributes(typeof(CLSCompliantAttribute))
       .OfType<CLSCompliantAttribute>()
       .Any(attr => !attr.IsCompliant))
    .Select(member => $"{member.Name}")
    .ToArray();

结果:

Equals                     Equals(UInt64 other)     
CompareTo                  CompareTo(UInt64 other)  
op_Implicit                operator
op_Implicit                -/- 
op_Implicit                -/-
op_Implicit                -/-
op_Explicit                -/-  
op_Explicit                -/-
op_Explicit                -/- 
op_Explicit                -/-  
op_LessThan                -/-
op_LessThanOrEqual         -/-   
op_GreaterThan             -/-
op_GreaterThanOrEqual      -/-  
op_Equality                -/-
op_Inequality              -/-
op_LessThan                -/-  
op_LessThanOrEqual         -/-   
op_GreaterThan             -/-
op_GreaterThanOrEqual      -/-  
op_Equality                -/- 
op_Inequality              -/-
.ctor                   BigInteger(uint value)
.ctor                   BigInteger(UInt64 value)
.ctor                   BigInteger(Byte[] value)

答案 1 :(得分:1)

查看BigInteger的MSDN文档,您可以在C#C++VB.NETF#(所有支持的CLI语言)中找到示例。< / p>

C#可用的类型通常在Microsoft.CSharp命名空间下定义:

  

Microsoft.CSharp名称空间包含支持以C#语言编写的源代码的编译和代码生成的类型,以及支持动态语言运行时(DLR)和C#之间互操作的类型

答案 2 :(得分:0)

MSDN页面说明

  

通用Windows平台
  自8月起可用    .NET Framework
  自4.0起可用   便携式班级图书馆
  支持:可移植.NET平台
  的 Silverlight的
  自4.0起可用    Windows Phone
自8.1开始

所以答案是