CORE 8.0 et constructeur primaire
Bonjour,
J'ai un problème avec un constructeur primaire (C#12).
Mon point de départ est le code suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| internal class AnonymousDataProvider
{
public AnonymousDataProvider(Configuration configuration)
{
faker = new Faker(configuration.Generation.Language.ToString().ToLowerInvariant());
}
public string LastName()
{
return faker.Name.LastName();
}
public string PhoneNumber()
{
return faker.Phone.PhoneNumber("0# ## ## ## ##");
}
private readonly Faker faker;
} |
Visual studio affiche le message "IDE0290 : Use primary constructor".
Le [Ctrl]+[;] me propose cette correction.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| internal class AnonymousDataProvider(Configuration configuration)
{
public string LastName()
{
return faker.Name.LastName();
}
public string PhoneNumber()
{
return faker.Phone.PhoneNumber("0# ## ## ## ##");
}
private readonly Faker faker = new (configuration.Generation.Language.ToString().ToLowerInvariant());
} |
Cela enlève bien le message mais affiche désormais cet avertissement :
S3604 : Remove the member initializer, all constructors set an initial value for the member
Cet avertissement me parait bizarre mais si je comprend bien, ça me dit que mon Faker n'a pas besoin d'être initialisé.
D'ailleurs le [Ctrl]+[;] me propose l'option "Remove redundant initializer".
J'essaie donc cette option.
Le code devient :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| internal class AnonymousDataProvider(Configuration configuration)
{
public string LastName()
{
return faker.Name.LastName();
}
public string PhoneNumber()
{
return faker.Phone.PhoneNumber("0# ## ## ## ##");
}
private readonly Faker faker;
} |
Mais du coup, j'ai les avertissements suivants :
S3459 : Remove unassigned field 'faker', or set its value
CS9113 : Parameter 'configuration' is unread.
Bref, il y a quelque chose qui m'échappe mais je ne parviens pas à savoir quoi.
Quelqu'un a-t-il une solution ?
Merci.