Weird behavior of conditional operator?
string sortedcolumn =
( dataGridView1.SortedColumn != null
? dataGridView1.SortedColumn.Name
: "username"
);
In above statment when dataGridView1.SortedColumn==null
I get exception of dataGridView1.SortedColumn
is null instead of getting value as "username"
to sor开发者_如何学Pythontedcolumn
var.
Any idea?
Possible causes:
dataGridView1
is a field or variable containing the value null.dataGridView1
is a property getter that returns null.SortedColumn
is a weird property getter that returns something non-null the first time but null afterwards. (Very unlikely because then merely looking at it in the debugger would change the result.)dataGridView1
,SortedColumn
, orName
is a property getter that throws the exception you are seeing.dataGridView1.SortedColumn
is of a type that overloadsoperator!=
, which throws the exception you are seeing.dataGridView1.SortedColumn.Name
is of a type that implementsimplicit operator string
, which throws the exception you are seeing.
精彩评论