1

I am trying to pass optional parameter to method and having hard times understanding why it doesn't work. As most popular answer here (How can you use optional parameters in C#?) says public void SomeMethod(int a, int b = 0). So I have tried the same below.

Can I get rid of else statement here? I mean can I use parameter that is otherwise ="" if not something else = optional? If it exists, method will get it, if not then skip it?

Here is code with else statement that works (https://dotnetfiddle.net/sy5FW5):

using System;

public class Program
{
    public static void Main()
    {

        int MyNumber = 6;
        string StringOne;
        string StringTwo;

        if (MyNumber != 5)
        {
            StringOne = "Number is not 5";
            StringTwo = "True that!";
        }
        else
        {
            StringOne = "";
            StringTwo = "";
        }
        CheckNumber(StringOne, StringTwo);
    }

    public static void CheckNumber(string StringOne, string StringTwo)
    {
        Console.WriteLine(StringOne + "\n" + StringTwo);
    }
}

Above code is what I need and it is working fine. My question is more like is there any better way?

Here is what I was thinking of, but it does not work:

using System;

public class Program
{
    public static void Main()
    {

        int MyNumber = 6;
        string StringOne;
        string StringTwo;

        if (MyNumber != 5)
        {
            StringOne = "Number is not 5";
            StringTwo = "True that!";
        }
        CheckNumber(StringOne, StringTwo);
    }

    public static void CheckNumber(string StringOne = "", string StringTwo = "")
    {
        Console.WriteLine(StringOne + "\n" + StringTwo);
    }
}

Variables are not assigned in: CheckNumber(StringOne, StringTwo);

I was thinking that I can make parameters optional by using string StringOne = "" but it does not seem to be the solution?

10101
  • 2,232
  • 3
  • 26
  • 66

3 Answers3

4

In your particular case, you don't need to make the parameters of your method optional, you just need to initialize your variables to the default value you want to use:

    int MyNumber = 6;
    string StringOne = "";  // <--
    string StringTwo = "";  // <--

    if (MyNumber != 5)
    {
        StringOne = "Number is not 5";
        StringTwo = "True that!";
    }
    CheckNumber(StringOne, StringTwo);

A word of caution: Please do not take that as an invitation to go ahead and explicitly initialize all your variables. This should be done only when there is a good reason to do so, because it increases the chance of "I forgot to set this variable to the correct value" type of bugs (which would otherwise be caught by the assignment check of the C# compiler).

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • This works, but obviously it is less efficient (by some tiny amount) than using if/else, since if MyNumber is not 5, the strings are first initialised to "" and then reassigned to other strings. I would advise the OP to keep their code as it is. – Matthew Watson Jun 05 '20 at 09:20
  • @MatthewWatson: That depends on the OP's use case. In many cases, readability and code simplicity are much more important than tiny performance micro-optimizations. – Heinzi Jun 05 '20 at 09:25
  • I agree that code clarity is important, which is why I personally prefer the explicit if/else in cases such as this. YMMV. – Matthew Watson Jun 05 '20 at 09:27
  • @MatthewWatson: I do agree that variable initializations should be used sparingly (see my current edit to my answer). – Heinzi Jun 05 '20 at 09:37
1

An alternative way to initialise pairs of items is to use the C#8 "tuple" syntax, for example:

int MyNumber = 6;

(string StringOne, string StringTwo) = 
    MyNumber != 5 ? ("Number is not 5", "True that!")
                  : ("",                ""          );

CheckNumber(StringOne, StringTwo);
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

The string is a reference type and the default value for all reference types is null.

Try to init the StringOne and StringTwo to String.empty

G.Mich
  • 1,607
  • 2
  • 24
  • 42