Advanced C ++ / CLI Options

Why can't I declare default arguments for member functions of managed types or common functions? C# 4.0 introduced Named and optional arguments ; for CLI ? there is a similar thing

I do not understand why it is impossible to declare such a method:

 void Optional(int argument = 0); 

And then when I call Optional(); , the compiler does not translate this call to: Optional(0); .

+7
source share
1 answer

It seems that the C ++ / CLI compiler does not generate the correct IL directive for it. It does not highlight the .param [1] = int32(0) directive, which C # uses to recognize default parameters. If you open the generated assembly in ILDasm, you will see it.

The compilation method will be to use the Optional and DefaultParameterValue attributes from the System::Runtime::InteropServices , but C # does not use them for the default parameters, so there is currently no easy way to create an overload.

Here you can ask a question about these attributes: stack overflow

+8
source

All Articles