I suggest you ...

C++ CLI support for optional parameters for using in C#

in C++ CLI is possible to define an optional parameter like

public void ProvaDefaults([Optional, DefaultParameterValue(true)] bool value)

but in this way the compiler don't use the DefaultParameterValue for setting the correct default value in the metadata (II.15.4.1.4 The .para m directive) that is visible in C#

so in C# the deault parameter is visible but only with the system default value (in the sample reported it is false instead of the wanted true)

4,020 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Anonymous shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    2 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • PhilippePhilippe commented  ·   ·  Flag as inappropriate

        This should be fixed. As some other feature that are supported by both standard C++ and by C#, it is hard to understand why it won't works in C++/CLI.

        As far as I know, this is supported in the CLR so it should be simple the compiler to generated the proper metadata. By the way, if I remember well, the other way also need to be fixed when using C# code from C++/CLI...

      • Marcel HaldemannMarcel Haldemann commented  ·   ·  Flag as inappropriate

        A Workaround to this:

        There is a trick to make this working (workaround).
        the magic word is nullable, as for nullable types the default is always "null" (.HasValue == false)

        Example:

        C++ CLI in header:

        String^ test([Optional] Nullable<bool> boolTest);

        C++ CLI in .cpp file:

        String^ YourClass::test(Nullable<bool> boolTest)
        {
        if (!boolTest.HasValue) { boolTest = true; }
        return (boolTest ? gcnew String("True") : gcnew String("False"));
        }

        to Test it in C#:

        MessageBox.Show(YourClass.test());

      Feedback and Knowledge Base