[英]How better check Request.QueryString string parameter for null?
I need explanations.. I using C#.NET to web applications, I always write: 我需要解释..我将C#.NET用于Web应用程序,我总是这样写:
string val = Request.QueryString["foo"];
and then 然后
if(!string.IsNullOrEmpty(val))
What's the difference: 有什么不同:
string val = Request.QueryString["foo"];
I was advised to do: 我被建议做:
string val = Request.QueryString["foo"] as string;
if(!string.IsNullOrEmpty(val))
What's the difference? 有什么不同?
The first is better: 第一个更好:
string val = Request.QueryString["foo"];
The second version returns null
if the result of the call is not a string, but you know it always will be a string because the QueryString
member has type NameValueCollection
. 如果调用的结果不是字符串,则第二个版本返回
null
,但是您知道它始终是一个字符串,因为QueryString
成员的类型为NameValueCollection
。 The indexer is defined to return a string
: 索引器被定义为返回一个
string
:
public class NameValueCollection : NameObjectCollectionBase
{
// ...
public string this[string name] { get; set; }
// ...
}
The as string
is redundant as Request.QueryString["foo"]
already is a string. as string
是冗余的,因为Request.QueryString["foo"]
已经是一个字符串。 (So there is no other difference than the second makes you look like you don't know your framework ;-) ) (所以没有其他区别,第二个让你看起来像你不知道你的框架;-))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.