Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference an unused parameter?

Back in the days of C/C++, Microsoft had a #define, which allowed programmers to reference an unused parameter. The declaration, part of windef.h, is:

#define UNREFERENCED_PARAMETER(P) {(P)=(P);}
#define UNREFERENCED_LOCAL_VARIABLE(L) {(L)=(L);}

Whatever the exact name and syntax, the line had the effect of

  1. Telling the compiler to not flag this unused parameter as a warning
  2. The later stages of the compiler was smart enough to not include the line in the binary (or so I recall)
  3. Visually tells the viewer that the unreferenced parameter was not an oversight.

Is there a similar syntax in C#?

Although it makes no difference for this question, but the DevExpress CodeRush Visual Studio add-in flags all unused parameters, even in event handlers, as a warning.

NOTE: As I stated in my comment, I do not want to use pragma blocks. The purpose is to add a line of code that references the parameter for warning sake but adds none to trivial overhead, like what the windef.h header file macro did.

like image 593
Sarah Weinberger Avatar asked Oct 17 '13 20:10

Sarah Weinberger


2 Answers

Maybe the discard _ is what you're looking for:

void Foo(string parameter)
{
   _ = parameter;
}
like image 143
Discard Avatar answered Nov 03 '22 23:11

Discard


Using the SuppressMessage attribute you can suppress warnings where ever you want:

[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "isChecked")]
[SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "fileIdentifier")]
static void FileNode(string name, bool isChecked)
{
   string fileIdentifier = name;
   string fileName = name;
   string version = String.Empty;
}

This also gives the reader an explicit understanding that this is intended behavior.

More on the SuppressMessage attribute.

like image 35
user2674389 Avatar answered Nov 04 '22 00:11

user2674389