我可以使用C#在ASP.NET应用程序状态中设置数组吗? [英] Can I Set An Array In ASP.NET Application State Using C#
问题描述
如何使用C#在ASP.NET的Application状态中保存数组。
是的,您可以将值存储在Application状态。应用程序状态将值存储在Object类型中。因此,您可以在州内存储任何类型的数据。前提是您将其正确地转换回实际类型。像这样,
string [] stringArray = { Afzaal, Ahmad, Zeeshan};
应用程序[ StringArray] = stringArray;
有关在应用程序状态下存储值的更多信息,请阅读: https://msdn.microsoft.com/en-us/library/94xkskdf(v = vs.140).aspx [ ^ ]
现在当你想提取值时,你只需使用它......
if (Application [ StringArray]!= null ){
// 检查比不安全检查并获取NullReferenceException
// cast to string []
string [] stringArray =( string [])应用程序[ StringArray];
// 获取值
string surname = stringArray [ 2 ]; // Zeeshan
}
有关从应用程序状态中提取值的更多信息,请阅读: https://msdn.microsoft.com/en-us/library/y8hhek39(v = vs.140).aspx [ ^ ]
检查键/值对是否存在不会花费很多钱。但肯定会保护您免受NullReferenceException
的影响。然后,您可以将值转换为正确的类型(,所以键入它...即使是自定义类型,例如class
或struct
等)。他们会工作。 : - )
How can I save an array in ASP.NET's Application state using C#.
Yes, you can store the values in Application state. Application state stores the value in Object type. So, you can store any kind of data in the state. Provided that you properly cast it back to the actual type. Like this,
string[] stringArray = { "Afzaal", "Ahmad", "Zeeshan" }; Application["StringArray"] = stringArray;
For more on storing the values in Application state please read: https://msdn.microsoft.com/en-us/library/94xkskdf(v=vs.140).aspx[^]
Now when you want to extract the values, you simply use this...
if(Application["StringArray"] != null) { // checking is safer than not checking and getting NullReferenceException // casting to string[] string[] stringArray = (string[])Application["StringArray"]; // get the value string surname = stringArray[2]; // "Zeeshan" }
For more on extracting the values from Application state please read: https://msdn.microsoft.com/en-us/library/y8hhek39(v=vs.140).aspx[^]
Checking if the key/value pair exists or not doesn't cost a fortune. But will surely protect you fromNullReferenceException
. Then, you can cast the value to the proper type (which so ever type it is... Even custom types such asclass
orstruct
etc.). They would work. :-)
这篇关于我可以使用C#在ASP.NET应用程序状态中设置数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!