Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
667 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...