3

In C# I can use the method Console.Error.WriteLine. This doesn't work in Powershell, instead I must write

[Console]::Error.WriteLine

Why square brackets, why the double colons?

ps. To be clear, I'm not interested in logging, I want to understand the syntax about types and objects and methods

1
  • 1
    That's always the challenge with funny syntax features. You can't Google it (easily)! Commented May 18, 2012 at 13:23

3 Answers 3

10

Brackets = access to a type

double colons = access to a static member of a type : [MyType] return a Type instance

ex:

c:> [System.Int32]

IsPublic IsSerial Name BaseType

-------- -------- ---- --------

True True Int32 System.ValueType

using the dot notation would only give you access to instance members of the Type instance (reflection related methods for most)...

c:\> [System.Int32].Parse("3")

Method call failed because [System.Runtype] does not have any "Parse" member

c:\> [System.Int32].AssemblyQualifiedName

System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

so :: is the way to access static members of a Class

c:\> [System.Int32]::Parse("3")
3
Sign up to request clarification or add additional context in comments.

Comments

5

The square brackets [] are Powershell's way of denoting a type. The double colons :: represent a static member or method of that type, where the normal period . represents an instance member or method.

Note that these were influenced by other .NET languages - C# uses the . for all member access, and C++ uses the :: for namespaces, subclasses, and static calls.

Comments

0

Watch it here - http://www.youtube.com/watch?feature=player_detailpage&v=jpswk-4ykcc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.