简体   繁体   中英

Powershell - difference between “-” (dash) and “.” (hyphen)

I am a C# programmer, and I have been learning powershell haphazardly (as needed) off and on. I have a couple of (hopefully) quick questions about the difference between using a dot and a hyphen in 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? I know a .NET array has a .Contains method ... is this the same thing? Is it available because of the public accessor? Does PS just "know" what a .NET array is?

What causes the -contains notation to be available?

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 .

With that in mind, it's important to remember that calling arr.Contains( ... ) in C# is syntactic sugar. The real syntax is actually Enumerable.Contains(arr, ... ) .

Something similar is going on here. In PowerShell, you have things called Cmdlets . Cmdlets make up the bulk of the language's idiomatic API. It's very common for Cmdlets to have a number of optional arguments that determine what the Cmdlet actually does. The syntax for Cmdlet arguments looks like this:

 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. 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:

$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. 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. 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.

That said, your comment to the question that -contains is operator is also very close. 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; it's not == . Rather, C#'s most significant equality operator is often obj.Equals() . Suddenly, that doesn't feel so different from obj.Contains() any more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM