Jump to content

Default Parameters

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Althaz

Althaz

    Newbie

  • Members
  • PipPip
  • 15 posts
I just started using C# today (generally speaking, seems pretty awesome) and when I tired to create a function with default parameters, I got an error.

How do you do this in C#?

Here an example of what I tried:


class Player

{

    public Player(string position = "MC")

    {

        //Code, etc went in here

    }

    //Rest of class

}

It gave me an error that said I couldn't define a parameter's value or something like that.

Didn't turn up anything with a search. ;).

P.S.
Nice forums.

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I agree, C# is neat. The reason you can't have a default value for your function parameter in C# is because C# doesn't support this. Check out the C# FAQ here for more information on why they chose not to support it: C# Frequently Asked Questions : Why doesn't C# support default parameters?

#3
Althaz

Althaz

    Newbie

  • Members
  • PipPip
  • 15 posts
Thanks for the quick reply, Jordan.

It does make sense, however I'm lazy so it is also kinda annoying :).

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I thought C# 4.0 was going to allow default parameters?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums