optional keyword in c#

  • Thread starter Thread starter Tapasvi Mehta
  • Start date Start date
T

Tapasvi Mehta

how to define the optional parameters for a method or
function in c# ?

I am trying to convert an application from VB.NET to c#,
and don't know how to do the above without overloading.

Thanks,

Tapasvi
 
Tapasvi said:
how to define the optional parameters for a method or
function in c# ?

You can't. The closest you can get is a params which allows for an
arbitrary number of parameters.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Tapasvi Mehta said:
how to define the optional parameters for a method or
function in c# ?

I am trying to convert an application from VB.NET to c#,
and don't know how to do the above without overloading.

You can't, you have to use overloading.
 
Tapasvi,

No, it is not. Basically, say you have a method like this:

public void DoSomething(int myParam)

If you want to have a default value, you would create an overload that
does this:

public void DoSomething()
{
// Call the overload, passing a default value of one.
DoSomething(1);
}

Hope this helps.
 
Thanks to all for helping out.
-----Original Message-----
Tapasvi,

No, it is not. Basically, say you have a method like this:

public void DoSomething(int myParam)

If you want to have a default value, you would create an overload that
does this:

public void DoSomething()
{
// Call the overload, passing a default value of one.
DoSomething(1);
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Is it possible to default to a value for a parameter ?


.
 
Back
Top