我想将变量类型的 var 传递给方法,其中 var 是一些分布
例如:
var t = new Chi(Double.Parse(textBox8.Text));
var t = new Cauchy(Double.Parse(textBox6.Text), Double.Parse(textBox7.Text));
和方法:
drawDensity(var t) {...t.Sample()..t.Density()..}
我想将变量类型的 var 传递给方法,其中 var 是一些分布
例如:
var t = new Chi(Double.Parse(textBox8.Text));
var t = new Cauchy(Double.Parse(textBox6.Text), Double.Parse(textBox7.Text));
和方法:
drawDensity(var t) {...t.Sample()..t.Density()..}
First, the var
is not a type.It is a way to tell the compiler to infer the type for you,instead of stating it explicitly.So specifying it as a parameter type is invalid.
Secondly, drawDensity
method should take a parameter that is a common type between Chi
and Chaucy
, for example a common interface or a base class.If you want to pass both types to your method that is the preferred way.
If there is no common type that both type implements then it should be object or dynamic.
Further reading