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,042 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 →

    We don’t have plans to enhance the C++/CLI language to support more recent CLR functionality. The goal for C++/CLI remains to enable a thin interop layer between your native code and managed code in your Windows Desktop applications, not to fully replicate all CLR capabilities.

    Thank you for your understanding. We are returning your votes, please put them to good use!

    — Visual C++ Team

    4 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...
      • Philippe 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 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