为什么的Debug.WriteLine不正确的格式字符串?字符串、不正确、格式、Debug

2023-09-07 15:07:23 作者:唯你不可取代

我有如下的的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);
 
oracle这个sql怎么不对,报 文字格式与字符串不匹配

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);