Skip to content

Check consistency of optional parameter default value #575

@thomaslevesque

Description

@thomaslevesque

When you implement an interface method with an optional parameter, specifying a different default value in the implementation has no effect when the method is called via the interface, so it's a potential bug. For instance:

interface IFoo { void Bar(string s = "hello"); }
class Foo : IFoo { public void Bar(string s = "hi") { Console.WriteLine(s); } }
IFoo foo = new Foo();
foo.Bar(); // will print "hello", not "hi"

I have written an analyzer for this (I know we're supposed to discuss it before actually doing it, but I wanted to check the feasibility before I mentioned it). I still have to write the code fix, but it shouldn't take long.

Severity : Warning
Category: Maintainability (I'm not really happy with that, but I can't find a more suitable category)
Diagnostic Id: CC0110

What do you think? Is this something we should include in Code Cracker?

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions