私は次の方法を持っています.voidがここでは無効であるというコンパイラエラーがあるため、以下の default(void) に入れることができるものがあるかどうかを知りたいです:
private void applyDefaultsIfNecessary(ApplicationConfiguration configuration)
{
var defaults = new Dictionary<Predicate<ApplicationConfiguration>, Action<ApplicationConfiguration>>()
{
// { rule, action } - if rule is true, execute action
{ (c) => c.ConnectionString == null , (c) => c.ConnectionString = "foo" },
{ (c) => c.OutputExcelFilePath == null, (c) => c.ConnectionString = "bar" },
{ (c) => c.OutputDirectory == null, (c) => c.OutputDirectory = "baz" }
};
//Nothing to select, but we want to loop throough the dict and invoke action, if rule is true.
//It is a pity there is no extension method called DoForEach on collections.
defaults.Select((item) => item.Key.Invoke(configuration) ? item.Value.Invoke(configuration) : default(void) );
}
三項演算子の代わりに if-else ステートメントを使用できること (または、ダミー メソッドを呼び出して void を返すことができること) に気付きました。また、Select 拡張メソッドは、void を返すラムダを好みません。型を推測できないと言っているようですが、もちろん、次のように型を指定すると、次のいずれかになります。
defaults.Select<ApplicationConfiguration, void>((item) => { if (item.Key.Invoke(configuration)) item.Value.Invoke(configuration); } );
言語設計の観点から、void または void である変数のデータ型を返すことができる式がない理由に興味がありました。