Raz*_*t4x 0 c# visual-studio-2010 c#-4.0
protected void MakeAutoComplete(ref Control control, IListSource dListSource)
{
MakeAutoComplete(ref control, dListSource, false);
}
protected void MakeAutoComplete(ref Control control, IListSource dListSource, bool isComboBox)
{
var curControl = (isComboBox) ? (control as ComboBox) : (control as TextBox);
// other
}
Run Code Online (Sandbox Code Playgroud)
上线var curControlVS给我的错误
Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Windows.Forms.ComboBox' and 'System.Windows.Forms.TextBox',我可以理解的错误,我知道有没有投BTW TextBox和ComboBox,但是这就是为什么我使用var摆在首位.所以有什么问题?为什么抱怨?
var它不是一个动态类型,它只是告诉编译器"你想出类型"并让它决定的一种方便的方法.虽然它是在编译时决定的,但它始终是单一类型,因此您无法在运行时将其设置为"此类型或该类型".