Newbie Question - Declaring method syntax

  • Thread starter Thread starter Jack Addington
  • Start date Start date
J

Jack Addington

in C# can I put a default value for a param in the declaration or do I need
to create two method calls? I have a method that 99% of the time I want to
pass false to but occasionally pass true. Do I have to have 2 methods?

i.e.)

public int testMethod(bool someParam default false)
{
....
}

instead of:

public int testMethod()
{
return testMethod( false );
}

public int testMethod( bool someParam )
{
...
}

thx
 
public int testMethod(bool someParam default false)
{
...
}

instead of:

public int testMethod()
{
return testMethod( false );
}

public int testMethod( bool someParam )
{
...
}


No you cannot have default parameters in C#.
 
The easy way to do this is using overloading:

public int testMethod(bool MyParam)
{
//do some work
}

public int testMethod()
{
this.testMethod(false);
}

99% of the time, you will call the second one. 1% of the time, you will
call the first one.

HTH,
--- Nick
 
The most compelling reason that I've seen to include default parameters
in C# is to make it easier to keep the XML documentation of several
versions of a method in sync. With overloading, it's tedious copy and
paste.
 
Back
Top