Skip to content

Discards C# 7.0correctnessreadability

Signal that parameters are intentionally being ignored.

Code analysis can be very useful including telling you which variables, parameters etc. are not being used.

Sometimes however you can not just remove the parameter in the case where the function has to meet an existing signature.

C# 7.0 allows you to name the parameters _ (the underscore symbol) to indicate that you are intentionally not using the parameter and will not reference it.

Code

C#
class PdfDocWriter : IDocWriter
{
    void WriteDocument(Document doc, PrintSettings _)
    {
        var filename = Path.Combine(doc.Filename, ".pdf");
        File.WriteAllBytes(filename, pdfService.Render(doc));
    }
}
C#
class PdfDocWriter : IDocWriter
{
    void WriteDocument(Document doc, PrintSettings printSettings)
    {
        var filename = Path.Combine(doc.Filename, ".pdf");
        File.WriteAllBytes(filename, pdfService.Render(doc));
    }
}

Notes

More information