How do you define a method with optional/default parameter?

Thanks

Answer this question

How do you define a method with optional/default parameter?

  • Ska Software

    What's your exact requirement Have you have a look on keyword param in C#

    Best Regards,



  • DETNYRE

    Maybe the documentation is buried somewhere.

    I just a want to implement a simple optional parameter in c#. Something like;
    int Function(int index, bool recurse = true)

    Its saying "Default parameter specifiers are not permitted."

    For some reason, overloads don't work quite well when you are writing COM. I want to experiment if optional parameter will work.

    Also just for regular programming, it will be nice to know if optional/default parameter is doable in C#.


  • Bander ALSHARFI

    This feature is not yet availble in C#! You already know Alternates so I'm not going on that side;

    See this:

    http://builder.com.com/5100-6373-1050652.html

    Cheers ;-)



  • Karlo

     JRQ wrote:
    Also just for regular programming, it will be nice to know if optional/default parameter is doable in C#.


    Look at the missing type1 to invoke default parameters as found in COM from either C# and VB....if that is what your question relates too...

    Other than that, you can give the appearance of default parameter by method overloading.



    void DoIt(int myValue) { DoIt(myValue, true); } // Acts as default paramter.

    void DoIt(int myValue, bool Operation) { ... }

     

  • How do you define a method with optional/default parameter?