C
C#13mo ago
AdiZ

✅ Foreach Loop Printing Multiple Chars from a String Simultaneously

string s = "Hello World";
foreach(char ch in s)
{
Console.WriteLine(ch);
}

string s = "Hello World";
foreach(char ch in s)
{
Console.WriteLine(ch);
}

Prints
He
l
l
o

W
o
r
l
d
He
l
l
o

W
o
r
l
d
Surely H and e shouldn't be printed on the same line? What am I misunderstanding here?
31 Replies
MODiX
MODiX13mo ago
Angius
REPL Result: Success
string s = "Hello World";
foreach(char ch in s)
{
Console.WriteLine(ch);
}

string s = "Hello World";
foreach(char ch in s)
{
Console.WriteLine(ch);
}

Console Output
H
e
l
l
o

W
o
r
l
d
H
e
l
l
o

W
o
r
l
d
Compile: 614.117ms | Execution: 47.159ms | React with ❌ to remove this embed.
Angius
Angius13mo ago
It works fine
AdiZ
AdiZOP13mo ago
Weird. Online C# compilers are evidently not the way to go.
Angius
Angius13mo ago
They do have their quirks Although the reputable ones like Sharplab should work just fine
AdiZ
AdiZOP13mo ago
Do I need to set this thread to resolved or something?
Angius
Angius13mo ago
You can use /close if you want
AdiZ
AdiZOP13mo ago
I'll check that out. Thanks.
Angius
Angius13mo ago
Ideally, though, just run the code locally
AdiZ
AdiZOP13mo ago
Ah perfect. Thanks for the super speed reply 🙂
Angius
Angius13mo ago
Much better experience
AdiZ
AdiZOP13mo ago
Yeah I'm struggling to make it work in VS Code, only works in VS 2022 right now. Could you help me figure it out?
Angius
Angius13mo ago
Sure $vscode
MODiX
MODiX13mo ago
Follow the instructions here on getting started with DevKit for C# in VSCode: https://code.visualstudio.com/docs/csharp/get-started
Get started with C# and .NET in Visual Studio Code
Getting Started with C# and .NET Development in Visual Studio Code
AdiZ
AdiZOP13mo ago
I don't know how to make a program run, it just says that I haven't created a project, but I've no clue how to do that.
Angius
Angius13mo ago
Ah, well, C# is project-based Not file-based like Python or PHP
AdiZ
AdiZOP13mo ago
Interesting. So how can I create one in VS Code?
Angius
Angius13mo ago
You can't run just a loose myprogram.cs file dotnet new command dotnet new console for example, will create a CLI project
AdiZ
AdiZOP13mo ago
In the VS Code console?
Angius
Angius13mo ago
Yep
AdiZ
AdiZOP13mo ago
Awesome gimme two secs I'll go try that
Angius
Angius13mo ago
It will create a Program.cs and MyCoolProject.csproj files, which is the minimum needed for a C# project to work
AdiZ
AdiZOP13mo ago
I imagine this will be an issue?
No description
Angius
Angius13mo ago
Huh I guess install Powershell...?
AdiZ
AdiZOP13mo ago
It's a school computer and powershell is blocked. Probably that? It works for Unity soo
Angius
Angius13mo ago
Ah, could be
AdiZ
AdiZOP13mo ago
Does Unity not use .NET?
Angius
Angius13mo ago
It uses its own weird fork of Mono, last I checked They are working on migrating to .NET 6, I believe
AdiZ
AdiZOP13mo ago
Is that the MonoBehavior identifier next to all the scripts' classes?
Angius
Angius13mo ago
But they're taking their time, .NET 8 came out today lol No, MonoBehavior would be the base class : is inheritance
AdiZ
AdiZOP13mo ago
Haha yeah I was about to say, why not at least .NET 7 Ahhhh so that's how inheritance works.
Angius
Angius13mo ago
class MyThing : MonoBehavior means MyScript inherits MonoBehavior
Want results from more Discord servers?
Add your server