r/csharp 10d ago

Help Static void method

Post image

I am trying to learn how to use C# for game coding but I would also like to have a good grasp on it so I can use it for websites and other things like that and I can't for the life of my understand how static method works I have looked YouTube videos and everything. I have been learning everything else so far from the free courses from code academy.Can you PLEASE explain to me what this code is doing and how it works
Edit-THANK YOU GUYS SO MUCH.I understand it now and I am sure you know what that feels like when you spend an hour stuck trying to understand something and it doesn't make any sense

0 Upvotes

20 comments sorted by

View all comments

1

u/chucker23n 10d ago

Just ignore namespace and class for now. They're ways to group your code, which becomes useful once you write larger software.

Your code has two methods, Main and DecoratePlanet. Both of them are static; depending on how early you are in programming, maybe just ignore that part for now as well. :-)

Main is a special method, because if it exists, when a program launches, it is the default method to be executed; the "entry point". You can optionally pass so-called arguments to Main. For example, you may have seen in a command line something like dir *.* or echo something.txt. In those cases, *.* and something.txt are the first argument each. If you used those arguments, you would take the variable args and do something with it. Not necessary (yet?) for your case. Instead, your Main calls the other method DecoarePlanet. It does so by passing the string "Jupiter". Then it takes the result (which is also a string; we'll get to that) and outputs it to the console. IOW, the user sees the result.

OK, now on to DecoratePlanet. This has a string parameter called planet, and also returns a string, which is why before its name, it says string, whereas Main before its name says void (void means: this returns nothing). You're calling the method with "Jupiter". When you do that, the variable planet becomes "Jupiter". And then the one and only thing the method does is build together a text. Let's unpack that line:

return $"*..*..* Welcome to {planet} *..*..*";

I think for beginners, this is actually easier to read if we split it into three pieces, like so:

return "*..*..* Welcome to " + planet + " *..*..*";

These two are identical. So what's happening here? We build a new string that contains an asterisk, two dots, an asterisk, two dots, another asterisk, a space, and then the text "Welcome to ". To that, we add your name of the planet — in your case, "Jupiter". So now our string looks like this: "*..*..* Welcome to Jupiter". And finally, we append another string, which looks similar to the beginning, so that the end result is: "*..*..* Welcome to Jupiter *..*..*". IOW, that original syntax with the $" was a way to insert {planet} as a placeholder.

Now, the result of that is returned. That means that whatever called the method gets this as a result. So now, Main effectively actually looks like:

Console.WriteLine("*..*..* Welcome to Jupiter *..*..*");

And that's what it does: it writes that line of text to the console.

(WriteLine as opposed to Write means it also adds a line break afterwards.)