问题描述
我有如下的的Debug.WriteLine
:
的Debug.WriteLine(元数据版本:{0},版本); //更新:版本是一个字符串
的输出是:
为什么字符串格式化这样?
我没有看到MSDN文档标识背后格式推理什么。我必须做到以下几点,以获得正确的格式输出:
的Debug.WriteLine(的String.Format(元数据版本:{0},版本));
由于版本
是字符串
,你再击中超负荷的WriteLine
接受一个类作为它的第二个参数。
虽然有任何数量的黑客来解决这个问题(我将包括下面几个,为了好玩),我会亲自preFER您的解决方案为明确确保字符串是preferable方式视为格式字符串。
其他一些哈克的解决方法:
的Debug.WriteLine(元数据版本:{0}版,);
的Debug.WriteLine(元数据版本:{0},(对象)的版本);
的Debug.WriteLine(元数据版本:{0},新的[] {版本});
的Debug.WriteLine(元数据版本:{0},版本,NULL);
I have the following Debug.WriteLine
:
Debug.WriteLine("Metadata Version: {0}", version); // update: version is a string
The output is:
Why is the string formatted this way?
I didn't see anything in MSDN documentation that identifies reasoning behind this format. I have to do the following to get a correctly formatted output:
Debug.WriteLine(string.Format("Metadata Version: {0}", version));
Since version
is a string
, you're hitting the overload of WriteLine
that accepts a category as its second parameter.
While there are any number of hacks to get around this behavior (I'll include a few below, for fun) I would personally prefer your solution as the preferable way of clearly ensuring that the string is treated as a format string.
Some other hacky workarounds:
Debug.WriteLine("Metadata Version: {0}", version, "");
Debug.WriteLine("Metadata Version: {0}", (object)version);
Debug.WriteLine("Metadata Version: {0}", new[] { version });
Debug.WriteLine("Metadata Version: {0}", version, null);
这篇关于为什么的Debug.WriteLine不正确的格式字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!