New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Classes: an uninitialized [string] property defaults to $null rather than the empty string #7294
Comments
Is this unexpected? I know that I would personally be very surprised to find string members initialized to empty strings. I would expect class members to have default values as they do in other .NET languages. Coercing null to empty string is already a confusing foot gun in other contexts, I wouldn’t want to see it extended to classes. |
The primary purpose of this issue is to show the lack of consistency. I suggested resolving the inconsistency toward PowerShell-like behavior, because, as demonstrated, in the majority of cases you already do get the surprising behavior in the context of custom classes - in line with non-class PowerShell behavior:
(Ideally, PowerShell itself should also default That said, in other respects classes are closer to C# behavior, not least syntactically. So I suppose resolving the inconsistency toward C#-like behavior (allowing assigning Both resolutions are technically breaking changes, so a third option is to simply live with the inconsistency and merely document it. I can't really speak to the guiding principles behind the behavior of PowerShell classes. |
Point taken, and I agree that it’s a bit inconsistent. I guess I’m just voicing a preference for aligning with C# in this context. As you note, in many ways, it already is. No one has ever had to learn about [NullString]::Value and been happy about it. I know it’s too late for that, but I’d hate to have it show up more than it already has to. |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
2 similar comments
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |
This is an inconsistency discovered by @alx9r:
If you define a class property of type
[string]
without assigning an initial value, its value is unexpectedly$null
, even though in all other contexts PowerShell defaults[string]
values to''
(the empty string) and even converts explicitly assigned$null
to''
.Steps to reproduce
Expected behavior
Actual behavior
That is, the uninitialized
[string]
property unexpectedly contains$null
.Environment data
The text was updated successfully, but these errors were encountered: