I'm designing a cmdlet using plain C#. Is it possible to define a default value for a parameter?
Script cmdlet:
[Parameter] [string] $ParameterName = "defaultValue"
Which is the equivalent for C#?
[Parameter]
public string ParameterName { get; set; }
With auto-implemented properties, you can't. You will need to create the actual getter and setter.
Something like this:
private string _ParameterName = "defaultvalue";
[Parameter]
public string ParameterName
{
get
{
return _ParameterName ;
}
set
{
_ParameterName = value;
}
}
Since C# 6.0 has been released:
[Parameter]
public string ParameterName { get; set; } = "defaultValue";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With