C# – IDE0060: Remove unused parameter

If you have a method with a parameter, and that parameter is not used in the method, then you’ll get the IDE0060 message telling you to remove the unused parameter.

Here’s an example of code that would trigger this message:

public class MessageSender
{
	public IClient Client { get; set; }
	public void Send(string message, bool encrypt)
	{
		Client.Send(message);
	}
}
Code language: C# (cs)

The encrypt parameter isn’t being used in the Send() method, triggering the IDE0060 message:

IDE0060 Remove unused parameter ‘encrypt’ if it is not part of a shipped public API

First, IDE0060 has a Suggestion level severity, which is why it shows up in the Messages tab. In my opinion, it should be a warning instead. The reason is because if this parameter is actually needed, then the fact that it’s not being used means there’s a bug and not just a code smell. It may even make sense to treat it like an error.

Second, there are two questions to ask yourself:

  • Is this parameter needed?
  • Is there external code using this method right now?

If the parameter is needed, but not being used, then this is a bug. If it’s not needed, and there’s no external code currently using it, then it’s a code smell.

In this article, I’ll go through a few options for dealing with the unused parameter problem.

Option 1 – Use the parameter

Is this parameter needed? If yes, then you have a bug, because the calling code is passing in the parameter expecting it to have some effect.

For example, take a look at the IDE0060 trigger code again:

public class MessageSender
{
	public IClient Client { get; set; }
	public void Send(string message, bool encrypt)
	{
		Client.Send(message);
	}
}
Code language: C# (cs)

The encrypt parameter is not being used. This is a problem if the calling code expects it to be used. For example, let’s say the calling code is passing in encrypt=true:

var messageSender = new MessageSender();
messageSender.Send("TOP SECRET - Hello World", encrypt: true);
Code language: C# (cs)

When this doesn’t encrypt the message, the developer is going to be surprised.

Either use the parameter or delete it. Don’t leave it there if it’s not being used at all.

Option 2 – Remove the parameter

Maybe you added the parameter thinking that you’ll use it later. Or maybe you refactored the method and now the parameter is not needed.

In any case, if you have an unused parameter and it’s not needed right now, then you may be able to remove it. If there is external code calling your method, then be very careful, since changing your method signature will break that code.

The simplest way to remove a parameter from a method is to use the Change signature functionality in Visual Studio.

  1. Right-click the method name.
  2. Click Quick Actions and Refactorings…
  3. Click Change signature…
  4. Select the parameter you want to remove, click Remove, then click OK:
Change Signature refactoring window in Visual Studio. Choose the parameter to remove, click Remove, then click OK.

After clicking OK, it’ll remove the parameter from the method and remove the parameter from calls to the method:

public class MessageSender
{
	public IClient Client { get; set; }
	public void Send(string message) //removed parameter
	{
		Client.Send(message);
	}
}

var messageSender = new MessageSender();
messageSender.Send("TOP SECRET - Hello World"); //removed passing in the parameter
Code language: C# (cs)

Option 3 – When you can’t remove the parameter, use the discard symbol

There could be scenarios where you can’t remove an unused parameter, but you want to indicate that the parameter is unused. To do this, you can change the parameter name to the discard symbol.

If you’re going to use the discard symbol, it’s a good idea to set a default value as well so the calling code doesn’t have to pass in values for these discarded parameters.

For example, let’s say you have the following method with two unused parameters:

public class MessageSender
{
	public IClient Client { get; set; }
	public void Send(string message, bool encrypted, bool reverse)
	{
		Client.Send(message);
	}
}

var messageSender = new MessageSender();
messageSender.Send("TOP SECRET - Hello World", false, false);
Code language: C# (cs)

Here’s how you’d change this to use discard symbols:

public class MessageSender
{
	public IClient Client { get; set; }
	public void Send(string message, bool _ = false, bool _1 = false)
	{
		Client.Send(message);
	}
}

var messageSender = new MessageSender();
messageSender.Send("TOP SECRET - Hello World");
Code language: C# (cs)

Because the default values are set for the parameters, the calling code doesn’t need to pass in anything. Note: Existing calling code doesn’t need to be changed. It could still pass in “false, false” and not break.

When you’re changing parameter names, it’s always best practice to use the Rename functionality in Visual Studio, because it will also update calling code that it using named parameters.

Be warned: If there is external code calling your method and it’s using named parameters, changing the parameter name will break their code. For example, let’s say the following is external code that’s using named parameters:

var messageSender = new MessageSender();
messageSender.Send("TOP SECRET - Hello World", encrypted: false, reverse: false);
Code language: C# (cs)

Changing the parameter names to the discard symbols will cause a compiler error in this external code since there are no parameters named encrypted or reverse. Always be cautious when refactoring your public code.

Option 4 – Ignore the message with a pragma

If you have unused parameters that you don’t need, and there is external code calling your method, then the safest option is to ignore the analyzer message with a pragma.

The only reason to do that is if you simply want to get rid of the message.

Here’s how to ignore the message with a pragma:

    public class MessageSender
    {
        public IClient Client { get; set; }
#pragma warning disable IDE0060 // Remove unused parameter
        public void Send(string message, bool _, bool reverse)
        {
            Client.Send(message);
        }
#pragma warning restore IDE0060 // Remove unused parameter
    }
Code language: C# (cs)

Leave a Comment