Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么';这种转换在Powershell中不能像在C#中那样工作吗?_C#_Powershell_Types - Fatal编程技术网

为什么';这种转换在Powershell中不能像在C#中那样工作吗?

为什么';这种转换在Powershell中不能像在C#中那样工作吗?,c#,powershell,types,C#,Powershell,Types,我正在尝试将计算值转换为uint16。为了这个例子,我已经硬编码了。在C#中,它起作用。但是,我认为Powershell中的相同代码失败了。考虑以下事项: 在powershell代码示例中,它生成: Cannot convert value "101398986" to type "System.UInt16". Error: "Value was either too large or too small for a UInt16." At D:\OneDrive\Desktop\Varibl

我正在尝试将计算值转换为uint16。为了这个例子,我已经硬编码了。在C#中,它起作用。但是,我认为Powershell中的相同代码失败了。考虑以下事项:

在powershell代码示例中,它生成:

Cannot convert value "101398986" to type "System.UInt16". Error: "Value was either too large or too small for a UInt16."
At D:\OneDrive\Desktop\VaribleCasting2.ps1:2 char:1
+ [uint16]$v = [uint16]$g
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastIConvertible
Exception calling "ToUInt16" with "1" argument(s): "Value was either too large or too small for a UInt16."
At D:\OneDrive\Desktop\VaribleCasting2.ps1:2 char:1
+ [uint16]$v = [convert]::ToUInt16($g)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : OverflowException
我试过[convert]::ToUInt16($g),它会产生:

Cannot convert value "101398986" to type "System.UInt16". Error: "Value was either too large or too small for a UInt16."
At D:\OneDrive\Desktop\VaribleCasting2.ps1:2 char:1
+ [uint16]$v = [uint16]$g
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastIConvertible
Exception calling "ToUInt16" with "1" argument(s): "Value was either too large or too small for a UInt16."
At D:\OneDrive\Desktop\VaribleCasting2.ps1:2 char:1
+ [uint16]$v = [convert]::ToUInt16($g)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : OverflowException
PowerShell(失败):

C#(成功):


我的期望是.NET在两个平台上都能产生相同的结果。谢谢你的帮助

您试图为16位整数指定一个数字
101398986
,这是完全不可能的,只要您使用的是16位,因为16位整数必须小于或等于65535

或者如果其有符号整数:-32767 因此,最好在您的案例中使用32位整数:

var g = 101398986;
UInt32 v = (UInt32)g;

如果您的数字必须小于或等于4294967295(对于32位整数)

您正试图为您的16位整数指定一个数字
101398986
,这是完全不可能的,因为只要您使用的是16位,16位整数就必须小于或等于65535

或者如果其有符号整数:-32767 因此,最好在您的案例中使用32位整数:

var g = 101398986;
UInt32 v = (UInt32)g;

如果您的数字必须小于或等于4294967295(对于32位整数)

Powershell默认情况下会进行算术检查(如Visual Basic),这与C#不同。这意味着您在溢出时会默认获取,而不是运行时静默地截断结果


您可能需要查看更多详细信息。

与C不同,Powershell默认情况下执行检查算术(如Visual Basic)。这意味着您在溢出时会默认获取,而不是运行时静默地截断结果


您可能需要查看更多详细信息。

我认为您在c#中没有得到真正的UInt16。看看这个>>
[uint16]::MaxValue
谢谢,我知道这些限制。我想问的是,为什么这在C#NET中有效,而在PowerShell.NET中却不起作用。很明显,这里发生了一些我不明白的事情。这显然不是相同的类型转换。请看一下c#中的实际结果-我敢打赌,该数字要么被截断,要么被强制转换为更大的
[int]
类型。[咧嘴笑]我不认为你在c#中得到了真正的UInt16。看看这个>>
[uint16]::MaxValue
谢谢,我知道这些限制。我想问的是,为什么这在C#NET中有效,而在PowerShell.NET中却不起作用。很明显,这里发生了一些我不明白的事情。这显然不是相同的类型转换。请看一下c#中的实际结果-我敢打赌,该数字要么被截断,要么被强制转换为更大的
[int]
类型。[咧嘴笑]