比较 Viewstate 和 SessionState
我将 ViewState["zzz"] 和 Session["zzz"] 设置为同一字符串。
当我使用 == 进行比较时,我得到错误。
当我执行 ViewState["zzz"].Equals(Session["zzz"]) 时,我得到了正确的结果。
在调试器中,它们的值显示相同的字符串,当我这样做时,
? ViewState["zzz"] == Session["zzz"]
我得到错误。
我认为 ViewState StateBag Item 默认为设置值,Session Item 也默认为该值?
I set ViewState["zzz"] and Session["zzz"] to the same string.
When I compare using == I get false.
When I do ViewState["zzz"].Equals(Session["zzz"]), I get true.
In the debugger, both their values show the same string, and when I do
? ViewState["zzz"] == Session["zzz"]
I get false.
I thought the ViewState StateBag Item defaults to the set value and the Session Item defaults to the value as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 ViewState 和 Session 都存储对象,因此在比较之前必须将它们转换为字符串,否则您将得到引用比较,而不是调用重载字符串运算符 == 。
Since both ViewState and Session are storing objects, you must cast them to
string
before comparing, otherwise you'll get a reference comparison instead of the overload string operator == called.