C# 应用程序[“键”和应用程序内容[“键”之间有什么区别?
C# 应用程序[“键”和应用程序内容[“键”之间有什么区别?,c#,asp.net,C#,Asp.net,Application[“temp”]=8应将值8设置为键temp 然而,Application.Contents[“temp”]=8 那么这两者之间有什么区别呢 非常感谢。内容属性是应用程序的默认属性,因此使用时: Application["temp"] = 8; 在引擎盖下,上述代码将更改为调用Application.Contents[“temp”]=8 编辑:我刚刚使用了Reflector,正如Greg指出的,Contents属性只返回当前HttpApplicationState对象的引
Application[“temp”]=8代码>应将值8
设置为键temp
然而,Application.Contents[“temp”]=8代码>
那么这两者之间有什么区别呢
非常感谢。内容属性是应用程序的默认属性,因此使用时:
Application["temp"] = 8;
在引擎盖下,上述代码将更改为调用Application.Contents[“temp”]=8
编辑:我刚刚使用了Reflector,正如Greg指出的,Contents属性只返回当前HttpApplicationState
对象的引用。不确定我的回答在这种情况下是否完全正确-有人能验证这一点吗
编辑:好的,我发现当你调用Application[“temp”]=8代码>或应用程序。内容[“temp”]=8代码>它实际上调用HttpApplicationState.Item。查看此IL:
.method family hidebysig instance void Page_Load(object sender, class [mscorlib]System.EventArgs e) cil managed
{
.maxstack 8
L_0000: nop
L_0001: ldarg.0
L_0002: call instance class [System.Web]System.Web.HttpApplicationState [System.Web]System.Web.UI.Page::get_Application()
L_0007: ldstr "Key"
L_000c: ldc.i4.8
L_000d: box int32
L_0012: callvirt instance void [System.Web]System.Web.HttpApplicationState::set_Item(string, object)
L_0017: nop
L_0018: ldarg.0
L_0019: call instance class [System.Web]System.Web.HttpApplicationState [System.Web]System.Web.UI.Page::get_Application()
L_001e: callvirt instance class [System.Web]System.Web.HttpApplicationState [System.Web]System.Web.HttpApplicationState::get_Contents()
L_0023: ldstr "Key"
L_0028: ldc.i4.8
L_0029: box int32
L_002e: callvirt instance void [System.Web]System.Web.HttpApplicationState::set_Item(string, object)
L_0033: nop
L_0034: ret
}
“Contents”属性的目的只是返回一个值
它只返回这个
,所以理论上你可以做Application.Contents.Contents.Contents.Contents[“temp”]=8代码>它也会做同样的事情
只需使用应用程序[“temp”]=8代码>下一个问题是“这真的有什么用?”简单的索引。我猜他们只是想让它看起来和感觉像是一个会话。