C
C#11mo ago
disparatebits

Issue with formatted WriteLine

I'm following along with C#12/.net8 and came across this snippet around Ch5. The first one works, the second one cannot parse args, this seems like something simple I'm just not getting. I can print bob.Born or the FavoriteAcientWonder(excuse the typo) but as soon as I add a 3rd arg it won't work.
WriteLine(format: "{0}'s fav wonder is {1}. Integer is {2}",
arg0: bob.Name,
arg1: bob.FavoriteAcientWonder,
arg2: (int)bob.FavoriteAcientWonder);


WriteLine(format: "{0}'s fav wonder is {1}. Integer is {2}. {0} was born on {3}",
arg0: bob.Name,
arg1: bob.FavoriteAcientWonder,
arg2: (int)bob.FavoriteAcientWonder,
arg3: bob.Born);
WriteLine(format: "{0}'s fav wonder is {1}. Integer is {2}",
arg0: bob.Name,
arg1: bob.FavoriteAcientWonder,
arg2: (int)bob.FavoriteAcientWonder);


WriteLine(format: "{0}'s fav wonder is {1}. Integer is {2}. {0} was born on {3}",
arg0: bob.Name,
arg1: bob.FavoriteAcientWonder,
arg2: (int)bob.FavoriteAcientWonder,
arg3: bob.Born);
15 Replies
Pobiega
Pobiega11mo ago
I strongly recommend using interpolated strings instead of WriteLine with format strings
Console.WriteLine($"{bob.Name}'s fav wonder is {bob.AncientWonder}. Integer is {(int)bob.AncientWonder}");
Console.WriteLine($"{bob.Name}'s fav wonder is {bob.AncientWonder}. Integer is {(int)bob.AncientWonder}");
leowest
leowest11mo ago
the superior way to WriteLine, approved, please submit the PR.
disparatebits
disparatebitsOP11mo ago
haha I will and I like that syntax, I was just curious why it didn't work and why ChatGPT/Bard also had no idea.
leowest
leowest11mo ago
but interesting the last one fails.
jcotton42
jcotton4211mo ago
Console.WriteLine Method (System)
Writes the specified data, followed by the current line terminator, to the standard output stream.
leowest
leowest11mo ago
I wonder if its a limitation of args as opposed to params
jcotton42
jcotton4211mo ago
because there's no 4-arg overload
leowest
leowest11mo ago
ah yeah that is what I was imagining
jcotton42
jcotton4211mo ago
I strongly recommend you stay well away from ChatGPT and similar until after you're comfortable with C# because it often spits out complete garbage
leowest
leowest11mo ago
I would suggest the same specially if you're learning
disparatebits
disparatebitsOP11mo ago
yeah i kinda get that feeling tbh. i am coming from java, i started learning spring boot but there are waaaay more c# jobs where i am and want to live. when they first came out i was very impressed and worried about the future, now I am mostly frustrated and angry unless it is dead-simple...
Pobiega
Pobiega11mo ago
asp.net is like spring boot but better :kekw: you'll probably like it
jcotton42
jcotton4211mo ago
LLMs are quite useful, but you need to actually understand the output to make use of them at least for me they're still in that stage where verifying the output is often more work than its worth
disparatebits
disparatebitsOP11mo ago
I appreciate the advice
Jimmacle
Jimmacle11mo ago
just remember all LLMs do is predict text they don't know how to program :when: that's usually what i use them for, autocomplete or making variations of some code based on something i already wrote or like a more chatty search engine

Did you find this page helpful?