When assigning a default value to a uint parameter in a C# method argument, as shown in the first code block below, I am presented with the message “A value of type ‘int’ cannot be used as a default parameter because there are no standard conversions to type ‘uint'”, whereas when assigning an int to a uint variable in the method body it is fine.
The code does compile; the warning is provided by means of the red, squiggly underlining in Visual Studio 2010.
void GetHistoricData(uint historyLength = 365)
{
uint i = 365; // this is valid
}
This is easily resolved by by using 365U in place of 365, thus:
void GetHistoricData(uint historyLength = 365U)
{
// method body
}
I am finding it really difficult to find a good explanation of why it is invalid to assign an int to the uint in the parameter when it is valid to do so elsewhere. Can anyone help me with the ‘light-bulb’ moment I am trying to find?
It compiles for me, with compiler versions of both 4.0.30319.1 and 4.0.30319.17379.
If you’re using an older version or a different compiler (e.g. a Mono one) then I suspect it’s simply a bug.
EDIT: If it’s compiling fine (and I can’t provoke a warning at all from the compiler) then I suspect it’s some plug-in, such as ReSharper. (Not that I’m seeing it in ReSharper either…)
I can’t reproduce this in VS2010.
I would suggest that you do update the code to the one without the warning, just for the sake of readability – but it’s still valid code without the change.