Hello,
Would anybody please explain to me why do these two "methods" work differently - I failed to find any information that would clarify this...
Thank you in advance,
Michael
Hello,
Would anybody please explain to me why do these two "methods" work differently - I failed to find any information that would clarify this...
Thank you in advance,
Michael
Hi,
Types commonly override the Object.ToString method to return a string that represents the object instance. For your above case, the type of $a1 is DateTime and the method is DateTime.ToSting that converts the value of the current DateTime object to its equivalent string representation. If the type doesn't override the ToSting method, it still calls the default Object.ToString() method. You can get the type using the GetType() method.
https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tostring
Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
why do these two "methods" work differently
Because they are different :-)
Object.ToString is a .NET method and the objects decides what to return as string representation. If the object has nothing meanfull to return, then it returns it's name.
Out-String is a PowerShell cmdlet, which collects all string properties of an object and returns it as one accumulated string; see Exmaple 1.
"If the object has nothing meanfull to return, then it returns it's name." - the problem is I can't understand/predict when this method has or does not have something "meanfull to return"...
In other words I don't understand why in my example the "PredictFailure False" can not be cast to string :(
Hi,
The explanation is given in the above documentation. The default implementation of the ToString method returns the fully qualified name of the type of the Object.
Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
"The default implementation of the ToString method returns the fully qualified name of the type of the Object."- that's exactly that l don't understand: I don't think users would like to use ToString just to get the "the fully qualified name of the type of the Object" - conversion to string is expected, not just displaying the name of the object.
Another example:
If the default implementation of the ToString method returns the fully qualified name of the type of the Object why in this case the real string is returned and not the name of the object as in the first example?
11 people are following this question.