我在Powershell中遇到了这个奇怪的问题(不是在其他语言中).有人可以向我解释为什么会这样吗?
我试图返回一个指定的数字(数字8),但该函数不断向我抛出一切.这是一个错误还是设计?
Function GetNum() {
Return 10
}
Function Main() {
$Number10 = GetNum
$number10 #1 WHY NO OUTPUT HERE ??????? I don't want to use write host
$result = 8 # I WANT THIS NUMBER ONLY
PAUSE
return $result
}
do {
$again = Main
Write-Host "RESULT IS "$again # Weird Result, I only want Number 8
} While ($again -eq 10) # As the result is wrong, it loops forever
Run Code Online (Sandbox Code Playgroud)
这是一个错误还是设计?
按设计.在PowerShell中,cmdlet可以返回对象流,就像yield return在C#中使用以返回IEnumerable集合一样.
该return不需要的关键字为要返回的输出值,它简单地退出(或从返回)当前范围.
从Get-Help about_Return(重点补充):
The Return keyword exits a function, script, or script block. It can be
used to exit a scope at a specific point, to return a value, or to indicate
that the end of the scope has been reached.
Users who are familiar with languages like C or C# might want to use the
Return keyword to make the logic of leaving a scope explicit.
In Windows PowerShell, the results of each statement are returned as
output, even without a statement that contains the Return keyword.
Languages like C or C# return only the value or values that are specified
by the Return keyword.
| 归档时间: |
|
| 查看次数: |
1046 次 |
| 最近记录: |