空字符串上的 ToString
为什么其中第二个会产生异常,而第一个却不会?
string s = null;
MessageBox.Show(s);
MessageBox.Show(s.ToString());
更新 - 我可以理解的异常,(对我来说)令人困惑的是为什么第一部分没有显示异常。这与消息框无关,如下所示。
例如:
string s = null, msg;
msg = "Message is " + s; //no error
msg = "Message is " + s.ToString(); //error
第一部分似乎隐式地将 null 转换为空白字符串。
Why does the second one of these produce an exception while the first one doesn't?
string s = null;
MessageBox.Show(s);
MessageBox.Show(s.ToString());
Updated - the exception I can understand, the puzzling bit (to me) is why the first part doesn't show an exception. This isn't anything to do with the Messagebox, as illustrated below.
Eg :
string s = null, msg;
msg = "Message is " + s; //no error
msg = "Message is " + s.ToString(); //error
The first part appears to be implicitly converting a null to a blank string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
因为您无法在
null
引用上调用实例方法ToString()
。MessageBox.Show()
可能是为了忽略 null 并打印出空消息框而实现的。because you cannot call instance method
ToString()
on anull
reference.And
MessageBox.Show()
is probably implemented to ignore null and print out empty message box.由于这个问题在 Google 上搜索“c# toString null”时排名相当高,因此我想补充一点,
Convert.ToString(null)
方法将返回一个空空字符串,消息框会忽略该字符串。但是,为了重申其他答案,您可以在此示例中使用
string.Concat("string", null)
。编辑 - 根据下面 HeyJude 的评论修改答案。正如所指出的,像
Convert.ToString(null).Length
这样的方法将引发异常。As this question ranks quite high on Google for a search for "c# toString null", I would like to add that the
Convert.ToString(null)
method would returnan emptya null string, which is ignored by the messagebox.However, just to reaffirm the other answers, you can use
string.Concat("string", null)
in this example.Edit - modified answer in line with HeyJude's comment below. As pointed out, a method like
Convert.ToString(null).Length
will throw an exception.这是因为MessageBox.Show()是用pinvoke实现的,它调用了原生的Windows MessageBox()函数。这并不介意 lpText 参数为 NULL。 C# 语言对纯 .NET 实例方法(如 ToString)有更严格的规则,它总是发出代码来验证对象不为 null。此 博客文章。
It is because MessageBox.Show() is implemented with pinvoke, it calls the native Windows MessageBox() function. Which doesn't mind getting a NULL for the lpText argument. The C# language has much stricter rules for pure .NET instance methods (like ToString), it always emits code to verify that the object isn't null. There's some background info on that in this blog post.
在您的后续问题/更新中调用幕后 concat 例如
Behind the scenes concat is being called in your follow up question / update E.g
您正在尝试对 null 执行 ToString() 方法。您需要一个有效的对象才能执行方法。
You are trying to execute the ToString() method on a null. You need a valid object in order to execute a method.
.show 函数必须进行 null 检查并处理它。
The .show function must have null checking and handle it.
ToString() 不能对 s vairable 的空引用进行操作
最短的方法
另一种正确的方法
https://www.informit.com/articles/article.aspx?p=2421572
ToString() can't operate on null reference of s vairable
The shortest way
Another correct ways
https://www.informit.com/articles/article.aspx?p=2421572
因为,第二次调用期望“s”对象满足 ToString() 方法请求。因此,在调用 .Show() 之前,s.ToString() 会因尝试调用方法而失败。
有趣的是,虽然 .Show() 正确实现,但许多此类方法期望传入非空实例。通常,这是当您使用 NullObject 模式,以便调用者不必处理这种行为。
Because, the second call is expecting an object of "s" to satisfy a ToString() method request. so, before .Show() is called, the s.ToString() would failed with an attempt to call a method.
Interestingly, While .Show() is implemented correctly, many such methods expect non null instances to be passed in. Usually, that is when you use a NullObject pattern so that the caller should not have to deal with this kind of behavior.
可能 Show 方法处理空值并且什么也不显示。
s - s.ToString() 的第二次使用失败,因为没有要运行的 ToString 方法。
Probably the Show method handles a null value and just shows nothing.
The second use of s - s.ToString() fails because you there is no ToString method to run.
正如您自己所说,它与
MessageBox
无关,它取决于ToString()
方法的实现。例如查看 struct Nullable其中 T : struct 来自
System.Runtime
,可空枚举用作示例:对于 .NET 7,它的
ToString()
实现如下所示:因此即使在
tc.TestEnum< 的 null 对象上调用
ToString()
,以下代码也会在MessageBox.Show(s.ToString());
上出现异常/代码>。从
System.Runtime
查看class String
:这里我们有另一个实现,它将抛出您看到的错误:
但是从
开始新项目默认启用 .NET 6<
enable
。https://learn.microsoft.com/en-us/dotnet/ csharp/nullable-references
因此上面的代码会给你两个警告:
string s = null;
-> CS8600 将 null 文字或可能的 null 值转换为不可为 null 的类型。MessageBox.Show(s.ToString() );
-> CS8602 取消引用可能为空的引用像这样重写代码,错误就会消失:
或者简单地使用 null 条件运算符
?.
就像 @mr R 写道:As you say yourself it has nothing to do with
MessageBox
, it depends on the implementation of theToString()
method.For example looking at
struct Nullable<T> where T : struct
fromSystem.Runtime
that a nullable enum uses as example:It's implementation of
ToString()
looks like this for .NET 7:Therefore the following code gives an exception on
MessageBox.Show(s.ToString());
even thoughToString()
is called on a null object fortc.TestEnum
.Looking at
class String
fromSystem.Runtime
:Here we have another implementation that will throw the error you see:
However starting with
.NET 6<
<Nullable>enable</Nullable>
is enabled by default for new projects.https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references
The code above will therefore give you two warnings:
string s = null;
-> CS8600 Converting null literal or possible null value to non-nullable type.MessageBox.Show(s.ToString());
-> CS8602 Dereference of a possibly null referenceRewrite the code like this and the errors disappear:
Or simply using the null-conditional operator
?.
like @mr R writes: