How to Skip Parameter in C#

Hello everyone, I need some information about function parameters in C#. Here is my situation: I have a singleton script called SubtitleManager. This script has a lot of other classes that are inherited from this, and has 1 main function to print subtitles to the screen. Most of the subtitles are the voices of the main character, but there are some other character’s subtitles. I want these character’s subtitles to be in different color than the main character’s. Since I have one main function to print subtitles, I used parameters.

protected void BypassSubtitle(string text, float duration, bool differentColor, Color newColor)

As you can see, we have a boolean variable, script changes the color of the subtitle to newColor variable, or default Color.white variable according to that boolean.

My question is, in some scripts, when I don’t want to change the subtitle color, I do not want to pass the last 2 parameters while calling the function. When I write:

BypassSubtitle("blabla", 3.0f);

I get errors that say the function can’t take two parameters, as it is obvious that I have 4 parameters so I have to do:

BypassSubtitle("blabla", 3.0f, false, Color.white);

So over here, eventhough I won’t be changing the color to another color, I still have to pass any Color parameter for the function to work, I am curious, can this cause overheads? Because actually I have 7-8 parameters, which are situation dependant, and also I have AudioManager which uses parameters too. So I though passing unnecessary parameters can cause performance issues, but I don’t know how not to call the function without the parameters that won’t be used. Any ideas? Thanks :slight_smile:

There are a couple of ways you can do this.

Default parameters:

 protected void BypassSubtitle(
     string text, float duration, 
     bool differentColor = false, Color newColor = Color.white);

 // edit: it turns out that you can't use Color.white or any of the other color
 // properties because they aren't constants that can be determined at compile time,
 // so you must use the default keyword instead...
 // the difference is that the default color will be black instead of white
 protected void BypassSubtitle(
     string text, float duration, 
     bool differentColor = false, Color newColor = default(Color));

Specifying the default value allows you to not pass the parameter.

Another way is to overload the method. You start with the one that takes all of the parameters, and in your overload you call that one, passing in defaults for the values that you don’t have…

// function with all the params
protected void BypassSubtitle(string text, float duration, bool differentColor, Color newColor)

// function with some of them
protected void BypassSubtitle(string text, float duration)
{
  BypassSubtitle(text, duration, false, Color.white);
}

protected void BypassSubtitle(string text)
{
  BypassSubtitle(text, 1.5f, true, Color.green);
}

If you go with the overloading, each overload must have a unique call signature, meaning it must be a unique combination of data types/parameter order.

Sorry, that didn’t work, let me try again:

public Class YourClass {
protected void BypassSubtitle(string text, float duration, Color newColor) 
{
 //Code1 
} 
protected void BypassSubtitle(string text, float duration) 
{ 
//Code2 
}
    
//then you can just do
   BypassSubtitle ("Text", 1.2f, color.red);/* (this will execute Code1 with the Parameters)*/
         //or
         BypassSubtitle ("Text", 1.2f);/*(this will execute code2) /*
}

You pass in you Code at Code1 and Code2 and it will decide which code to execute based on the Amount and Type of Parameter you’ve give into it.
You can in Theory have infinite of those Methods with the same name but other Parameters. (This is called Overloading a Method) Hope this helps, its my first post :smiley: