Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Flag optional arguments on methods that differ from their interface declarations

Aperta
#102 5 commenti 0 reazioni 4 assegnatari Vedi su GitHub

@GrantErickson ci sta già lavorando.

Dal 10/4/2020.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

documentation

Consider the following case:

public class Foo : IFoo
{
    public void Bar(int value = 2) => Console.WriteLine(value);
}
public interface IFoo
{
    public void Bar(int value = 1);
}

public static void Main(string[] args)
{
    IFoo foo1 = new Foo();
    foo1.Bar();
    var foo2 = new Foo();
    foo2.Bar();
}

Output:

2
1

I would propose that the coding standard be

DO NOT: declare optional arguments on class methods that are implementing an interface.

Rational:

  • If these value are not in sync it will cause behavior as shown above.
  • It is possible to declare a constant and share it in both places. However, if you have an interface for accessing the class, why is there code bypassing it? If you want to invoke the method using the default value, simply go through the interface.
Lingua principale
C#
Stelle
12
Fork
16
Merge medio
2m
PR unite (30g)
10

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di IntelliTect/CodingGuidelines

Tutte le issue di IntelliTect/CodingGuidelines

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.