2

此代码工作正常

Dim bc As Long = Math.Pow(36, ((IBase36.Length - 1) - i))

!在 VB Math.Pow 下返回 DOUBLE 数据类型。

当我将它转换成 C# 时,我得到了

long bc = Math.Pow(36, ((IBase36.Length - 1) - i));

我有一个演员语法问题。

怎么可能解决?

4

4 回答 4

2
long bc = (long)Math.Pow(36, ((IBase36.Length - 1) -i));

() 是C#中的强制转换运算符。

于 2012-07-30T18:38:51.967 回答
2

可能在 VB 中,您有Option Strict Off或根本没有声明(默认关闭)

来自 MSDN

Visual Basic allows conversions of many data types to other data types. 
Data loss can occur when the value of one data type is converted to a data type with 
less precision or smaller capacity. A run-time error occurs if such a narrowing 
conversion fails. Option Strict ensures compile-time notification of these narrowing 
conversions so they can be avoided.

所以我将更改VB代码

Option Strict On

Dim bc As Long = CType(Math.Pow(36, ((IBase36.Length - 1) - i)), Long)
于 2012-07-30T18:43:34.073 回答
1

Math.Pow具有返回类型double,在 C# 中不能隐式转换为long,因此必须通过类型转换显式完成。我不精通 VB.NET,但那里的转换规则可能不那么严格。

于 2012-07-30T18:39:19.540 回答
0

在 C# 中,如果你想要一个结果,你必须告诉编译器 Math.Pow 是 long 或 double 类型。

在控制台应用程序中检查这一点。

int value = 2;
string power = "6";
Console.WriteLine("" + (long)Math.Pow(value, (Convert.ToInt16(power) - 1)));
Console.ReadKey();
于 2012-07-30T19:08:31.593 回答