为什么的Debug.WriteLine不正确的格式字符串? [英] Why does Debug.WriteLine incorrectly format strings?

查看:642
本文介绍了为什么的Debug.WriteLine不正确的格式字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下的的Debug.WriteLine

 的Debug.WriteLine(元数据版本:{0},版本); //更新:版本是一个字符串
 

的输出是:

  

2.0:元数据版本:{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:

2.0: Metadata Version: {0}

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不正确的格式字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆