1

Is there a Optional in C#?

Sub notify(ByVal company As String, Optional ByVal office As String = "QJZ")

How would you implement the above code in C#? I have seen optionalstr optionalint but what about other datatypes and custom object?

Charles
  • 50,943
  • 13
  • 104
  • 142
Flood Gravemind
  • 3,773
  • 12
  • 47
  • 79
  • 1
    Described often here at SO for example: http://stackoverflow.com/questions/199761/how-can-you-use-optional-parameters-in-c – thmshd Jul 05 '13 at 11:33
  • `notify(string company, string office = "QJZ")` <= `office` is optional if you equal it to a constant. – Corak Jul 05 '13 at 11:33
  • Full reference: http://msdn.microsoft.com/en-us/library/dd264739.aspx – thmshd Jul 05 '13 at 11:34
  • Named and Optional parameters: http://msdn.microsoft.com/en-us/library/dd264739.aspx – Riv Jul 05 '13 at 11:34
  • Note that the documentation indicates the parameters must be set to a constant, a value type or the default of a value type - which means you can't do this with custom objects (classes) and other reference types. – Tim Jul 05 '13 at 11:37
  • 1
    Too many Eric Cartmans with authoritaaahhhhh(downvote btn) – Flood Gravemind Jul 05 '13 at 11:59
  • @FloodGravemind - With all due respect, you could have answered your question with a simple google search in the same amount of time it took to post it. Hence the downvotes. – Tim Jul 05 '13 at 12:07
  • 1
    I don't think a question is worth a down-vote because the answer is easy, or because the question is a duplicate. Its a very straight-forward question with a direct answer, so it's technically a good SO question, and should be upvoted and answered. Since it is a duplicate, just flag it as such and move along. – Kratz Jul 05 '13 at 15:04

5 Answers5

7

You have to give the parameter a default value and it must be at the end (The last parameter).

public void Test(string param1, string optional = "") {

}

To put it into context:

public void Notify(string company, string office = "QJZ") {

}

http://msdn.microsoft.com/en-us/library/dd264739.aspx

Darren
  • 68,902
  • 24
  • 138
  • 144
2
public void MyMethod(string company, string office = "QJZ")
{

}

Remember that optional parameters have to come at the end of the signature and that if you have another method with the same name that doesn't take any optional parameters, the compiler will choose that other method by default and will not use the optional parameter one. For example, if you also had

public void MyMethod(string company)
{
} 

and you called

MyMethod("company name");

The compiler will automatically use the MyMethod(string company) overload.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
1

Remove the word optional and then it will take the default value. As long as you are using >= VS2010 See here There is similar question here

Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

try this

void notify(string company ,string office ="QJZ"){}
kostas ch.
  • 1,960
  • 1
  • 17
  • 30
0

Take a look at the documentation for the C# reference: http://msdn.microsoft.com/en-us/library/dd264739.aspx

justin.lovell
  • 675
  • 3
  • 11