String.Format: Allow variables for specifying the alignment component #50079
Unanswered
robertmclaws
asked this question in
Q&A
Replies: 2 comments
-
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Beta Was this translation helpful? Give feedback.
0 replies
-
@danmoseley Question for you: out of curiosity, what was the rationale behind moving this to the discussions section? It was not meant to be a question, but a feature request for .NET 6.0. Thanks! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When formatting a string in C#, you can specify an alignment parameter by adding a comma and then specifying either a positive or negative number.
The problem is that this number must be a constant, which makes it very difficult to format large sections of data where you don't know the length.
To demonstrate a real-world situation, let's consider the need to output the contents of a DependencyInjection container to a text file for unit testing.
That's kind of a pain in the butt to read. What I really want is for it to look like this:
Consider this code that outputs the contents of a IServiceCollection to a string, which should work: (the
AppendLine
inside theForEach
)But for some reason, this is not possible. This forces us to have to include a function to generate the string format, which makes the code considerably more complicated:
There has to be a way to allow that parameter to be a variable. Any chance we could make it happen? Thanks so much for your consideration!
Beta Was this translation helpful? Give feedback.
All reactions