c# - Error : possible unintended reference comparison to get a value comparison cast the right hand -
i have following code gives warning "possible unintended reference comparison; value comparison, cast right hand side type string
":
// ocontrol of type control if ((ocontrol.name == odatarowview["conname"].tostring())) { //do stuff } else { //do other stuff }
i've tried fix using each of following still warning
if ((ocontrol.name == convert.tostring(odatarowview["conname"]))) { //do stuff } else { //do other stuff } if ((ocontrol.name == (string)odatarowview["conname"])) { //do stuff } else { //do other stuff }
please explain reason still warning , best practice way deal this?
these options work:
if (equals(ocontrol.name, odatarowview["conname"])) // best option if ((string)ocontrol.name == odatarowview["conname"].tostring()) // assuming neither item null if (ocontrol.name string == odatarowview["conname"] string) // assuming second item string
Comments
Post a Comment