简体   繁体   English

在Uri对象上,原始字符串属性是否可以为null?

[英]On the Uri object, can the original string property ever be null?

Is it possible to construct the Uri object in such a way that it has a null original string? 是否可以以具有空原始字符串的方式构造Uri对象?

Ie Is the Uri myUri.OriginalString != null check worthy? Uri myUri.OriginalString != null检查值得吗?

The MSDN for OriginalString notes that: MSDN for OriginalString指出:

An InvalidOperationException is possible if This instance represents a relative URI, and this property is valid only for absolute URIs. 如果此实例表示相对URI,并且此属性仅对绝对URI有效,则可能发生InvalidOperationException However I haven't been able to trip this. 但是我还不能绊倒。

new Uri("~/SignIn", UriKind.Relative) seems to work OK for OriginalString. new Uri("~/SignIn", UriKind.Relative)对于OriginalString似乎工作正常。

From MSDN : an ArgumentNullException is thrown at construction if the Url string is null (also if it is empty, ill-formed etc..). MSDN :如果Url字符串为null (也为空,格式错误等),则在构造时抛出ArgumentNullException

So the check is not needed. 因此不需要检查。

http://msdn.microsoft.com/en-us/library/vstudio/system.uri.originalstring says this property contains the exact URI specified when this instance was constructed; http://msdn.microsoft.com/zh-cn/library/vstudio/system.uri.originalstring表示此属性包含构造此实例时指定的确切URI; otherwise, Empty. 否则,为空。

http://msdn.microsoft.com/en-us/library/vstudio/z6c2z492 specifies Uri can't be constructed with null string. http://msdn.microsoft.com/zh-cn/library/vstudio/z6c2z492指定不能使用空字符串构造Uri。

So, you need not check against null. 因此,您无需检查null。 But you may want to check against String.Empty though. 但是,您可能要检查String.Empty。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM