They both sound similar. From msdn:
Gets a value indicating whether this parameter is optional.
This method depends on an optional metadata flag. This flag can be inserted by compilers, but the compilers are not obligated to do so.
This method utilizes the Optional flag of the ParameterAttributes enumerator.
ParameterInfo.HasDefaultValue (new in .NET 4.5)
Gets a value that indicates whether this parameter has a default value.
Aren't they the same? I did quick test:
public void A(string value)
{
}
public void B(string value, int i = -1)
{
}
I wrote:
var a = AInfo.GetParameters().Select(p => p.HasDefaultValue).ToArray();
var b = AInfo.GetParameters().Select(p => p.IsOptional).ToArray();
var c = BInfo.GetParameters().Select(p => p.HasDefaultValue).ToArray();
var d = BInfo.GetParameters().Select(p => p.IsOptional).ToArray();
//a equals b; and c equals d
So in which context are they different? Why did BCL introduce HasDefaultValue
in .NET 4.5 newly?