[英]Powershell - difference between “-” (dash) and “.” (hyphen)
I am a C# programmer, and I have been learning powershell haphazardly (as needed) off and on. 我是C#程序员,并且我一直在随意(根据需要)不断学习Powershell。 I have a couple of (hopefully) quick questions about the difference between using a dot and a hyphen in powershell.
我有几个(希望)简短的问题,关于在Powershell中使用点和连字符之间的区别。
For example, in the case of an array. 例如,在数组的情况下。
$arr = @("sam", "mitch", "stan") #create array
$arr.Contains("sam") #contains, apparently .NET?
$arr -contains "sam" #contains, apparently PS?
What is the difference between the "contains" calls? “包含”调用之间有什么区别?
What causes the .Contains() to be available? 是什么原因导致.Contains()可用? I know a .NET array has a .Contains method ... is this the same thing?
我知道.NET数组具有.Contains方法...这是同一回事吗? Is it available because of the public accessor?
是否可以使用公共访问器? Does PS just "know" what a .NET array is?
PS只是“知道” .NET数组是什么吗?
What causes the -contains notation to be available? 是什么导致-contains符号可用?
Is there a reason to use one over the other? 是否有理由使用一个?
Thanks! 谢谢!
Coming from the C# world, let's pretend for a moment that Contains()
is just an extension method on Enumerable
... something not far from the truth . 来自C#世界,让我们假装一下,
Contains()
只是Enumerable
的扩展方法…… 与事实相差不远 。
With that in mind, it's important to remember that calling arr.Contains( ... )
in C# is syntactic sugar. 考虑到这一点,重要的是要记住在C#中调用
arr.Contains( ... )
是语法糖。 The real syntax is actually Enumerable.Contains(arr, ... )
. 真正的语法实际上是
Enumerable.Contains(arr, ... )
。
Something similar is going on here. 类似的事情正在发生。 In PowerShell, you have things called
Cmdlets
. 在PowerShell中,您拥有称为
Cmdlets
东西。 Cmdlets make up the bulk of the language's idiomatic API. Cmdlet构成了该语言惯用API的大部分。 It's very common for Cmdlets to have a number of optional arguments that determine what the Cmdlet actually does.
Cmdlet具有许多可选参数来确定Cmdlet实际执行的操作是很常见的。 The syntax for Cmdlet arguments looks like this:
Cmdlet参数的语法如下所示:
CmdletName -argumentname "optional argument parameter"
Now, I know this code ( $arr -contains "sam"
) doesn't exactly follow that pattern, but it's a better way to understand what powershell is doing. 现在,我知道这段代码(
$arr -contains "sam"
)并不完全遵循该模式,但这是了解powershell所做的更好的方式。 As far as powershell goes, the most idiomatic code for someone who learned it first would be to use the "native" cmdlet argument-style syntax: 就powershell而言,最先了解它的人最惯用的代码是使用“本机” cmdlet参数样式语法:
$arr -contains "sam"
I put native in scare quotes here, becuase Powershell ultimately relies a great deal on .Net, and likely it's calling the exact same .Net Contains()
method in much the same way that .Net would: lookup the $arr
object reference to resolve and call it's Contains()
function. 我在这里用native引用,因为Powershell最终很大程度上依赖于.Net,并且可能以与.Net几乎相同的方式调用完全相同的.Net
Contains()
方法:查找$arr
对象引用以进行解析并调用它的Contains()
函数。 It may even be that, when it comes down to it, $arr.Contains("sam")
is the more direct path for the Powershell execution environment to produce the desired function call. 甚至可能是
$arr.Contains("sam")
是Powershell执行环境生成所需函数调用的更直接路径。 However, when in Rome... I try to use the natural powershell syntax when possible, even if, as a fellow C# user, the other way feels much more natural to me and I don't always succeed. 但是,在罗马时...即使我作为C#的其他用户,我也会尝试使用自然的Powershell语法,即使对我来说,另一种感觉也很自然,但我并不总是成功。
That said, your comment to the question that -contains
is operator is also very close. 也就是说,您对
-contains
是运算符的问题的评论也非常接近。 Take a look at the other powershell equality operators: -eq
, -ne
, - lt
, -gt
, -le
, -ge
, -like
, etc. To help them feel more familiar, let's go back to C#, and look at how C# really does equality; 看看其他的PowerShell相等运算符:
-eq
, -ne
, - lt
, -gt
, -le
, -ge
, -like
等,让他们感受到更多的熟悉,让我们回到C#,看看如何C#确实做到平等; it's not ==
. 不是
==
。 Rather, C#'s most significant equality operator is often obj.Equals()
. 相反,C#最重要的相等运算符通常是
obj.Equals()
。 Suddenly, that doesn't feel so different from obj.Contains()
any more. 突然之间,这与
obj.Contains()
并没有什么不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.