Session and ViewState Comparison with String

4

I recently received the following Visual Studio warning in code similar to the image:

Iunderstandwhathewantedtotellme:thereisareferencecomparisonofthetwovalues(ofthesessionandofthe"test" string), and this will always return false , since any value stored in Session will never be referenced in the right-hand string.

But why does this happen? Why Visual Studio was not able to do the comparison without a previous conversion?

PS: The same happens with ViewState

    
asked by anonymous 08.03.2017 / 18:20

1 answer

3
  

Why Visual Studio was not able to do the comparison without a previous conversion?

Because the return of Session["teste"] is object , not string . Not necessarily everything in Session is string , so notice. OC # tries to compare two objects, which causes a fallback operator, changing the nature of the comparison for reference .

comment , use Session["teste"].ToString() to compare correctly.

    

08.03.2017 / 18:29