Forgive my never ending questions yet again jrv. I am always either learning new and right or unlearning bad; either way I am good.
By default cast which you say calls the default constructor, is that not a public parameterless instance constructor for a class or type?
My understanding is that it is a special method of a class or type that instantiates a new copy with base property values where none are supplied or not required.
Foe example, $NewVersionVariable = New-Object System.Version created a new instance of theSystem.Version object
on my PC in $NewObj variable with the values:
Major Minor Build Revision
----- ----- ----- --------
0 0 -1 -1
I never supplied the ArgumentList parameter with values, the default constructor did, or is that not so? Please correct me if I am wrong. I really want to be sure my understanding of these
things are sound.
Similarly, $NewDateVariable = New-Object System.DateTime gave me a $NewDate variable ofDateTime type
with a value of January 1, 0001 12:00:00 am on my PC
.Net MSDN specifically advised against default constructors for structure type like DataTime until this year's release as some compilers (C#, VB, F#) did not support it. Which may be thee
reason why Powershell will not regard creating a variable without explicitly initializing it. In other words, the following will fail:
[System.Version]$NewVersionVariable
[System.DateTime]$NewDateVariable
Powershell will recognize the variables only if they are initialized by definite value assignment that it can cast by the accelerator
Moreover, of all the 11 instance constructors of DateTime, there is none that takes a string input. So there is no way casting can occur by constructor where a string parameter is provided.
See: DateTime Structure
With [DateTime]"23/1/2015" , I am not creating a new instance of an object, rather I am converting a string value - "23/1/2015" - to a DateTime object
I must then ask if there really is a difference between casting and conversion? I thought casting is just a technical word for conversion from one type to another. It simply describes the act of the conversion not the
means or mode. Parsing however is just one of the modes of conversion. In fact, Parse conversion (conversion based on Parse() method defined in the destination type) is said to be preferred by Powershell before Cast conversion (conversion
by an implicit or explicit cast operator) and also before Constructor conversion. So even if powershell will use a constructor of [DateTime] to a string, it will be AFTER it has attempted its Parse() method and failed.
[DateTime]::Parse(...) is indeed a call to a static method. But to do what? To convert, which is still casting, the difference now is just that I am explicit about HOW the casting is
done. In other words I am manually casting by calling a static method of the DateTime class.
Scripting Guys Blog says [datetime]"1/2/14 is using [DateTime] type accelerator
[psobject].Assembly.GetType("System.Management .Automation.TypeAccelerators")::get is a command that retrieves list of Type Accelerator in Pwershell and [DateTime] is of those listed
See: Convert String into DateTiime object
So how is [DateTime] not then a type? I am rather confused when you say it is a structure and not a type. MSDN says a structure is a value type. .Net types are either value types or reference types
See: Structure and Common
Type System