'base' as a function parameter name in C#
I am currently following Microsoft's Naming Guidelines, and so using camelCase in function parameter naming. Now suppose I would like to use the signature
public string WriteNumberInBase (int number, int base)
in some method and the compiler is complaining about the p开发者_高级运维arameter name just because 'base' is a reserved keyword... Is there any way I can get 'base' to be accepted as a parameter name?
Try this:
public string WriteNumberInBase (int number, int @base)
// ^
// the @ sign is used to "escape" keywords
(As a side note, in VB.NET you would do the same by putting a keyword or reserved word in square brackets, e.g. [MyBase]
.)
Or alternatively, simply call your parameter radix
instead of base
.
You can use @base
, as others have mentioned.
However, you could also follow the example of Convert.ToString(int, int) which uses toBase
as the parameter name.
Alternatively, you could use radix
as a synonym (in context) to base. At that point you might want to change the name of the method too, of course.
Yup.
public string WriteNumberInBase (int number, int @base)
You'll have to refer to the parameter with @
sign as well:
DoSomethingWith (@base);
This looks odd and I advise you to think of a different name.
It looks particularly odd when it's not the only parameter because @
sign confuses the brain into thinking this parameter is special in some way, when semantically it is not.
According to the first paragraph on this page, you can prepend the "@" character to create a valid identifier:
public string WriteNumberInBase(int number, int @base)
use @base instead of base .... it applies to all keywords ... use @ before the keyword and you will be fine
精彩评论