locked
Seting a default values for Parameter Methods RRS feed

  • Question

  • Hi!

    How set a default value for parameters on C# Methods ?

    Sample:

     public int mymethod (int param1 =0, int param2 = 1, int param 3 = 1) {....}

    This declaration show the CS0241 error.

    ???

    thanks.

    Sunday, January 29, 2006 1:21 AM

Answers

  • C# does not support default parameters. However you can fake it by using overloading:

    public int mymethod(int param1, int param2, int param3) {...}

    public int mymethod(int param1, int param2) {
        mymethod(param1, param2, 1);
    }

    public int mymethod(int param1) {
        mymethod(param1, 1, 1);
    }

    public int mymethod() {
        mymethod(0, 1, 1);
    }

    Sunday, January 29, 2006 5:41 AM