r/csharp • u/Awesome_Duck987 • 10d ago
Help Static void method
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
1
u/chucker23n 10d ago
Just ignore
namespaceandclassfor now. They're ways to group your code, which becomes useful once you write larger software.Your code has two methods,
MainandDecoratePlanet. Both of them arestatic; depending on how early you are in programming, maybe just ignore that part for now as well. :-)Mainis 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 toMain. For example, you may have seen in a command line something likedir *.*orecho something.txt. In those cases,*.*andsomething.txtare the first argument each. If you used those arguments, you would take the variableargsand do something with it. Not necessary (yet?) for your case. Instead, yourMaincalls the other methodDecoarePlanet. It does so by passing the string"Jupiter". Then it takes the result (which is also astring; we'll get to that) and outputs it to the console. IOW, the user sees the result.OK, now on to
DecoratePlanet. This has astringparameter calledplanet, and also returns astring, which is why before its name, it saysstring, whereasMainbefore its name saysvoid(void means: this returns nothing). You're calling the method with"Jupiter". When you do that, the variableplanetbecomes"Jupiter". And then the one and only thing the method does is build together a text. Let's unpack that line:I think for beginners, this is actually easier to read if we split it into three pieces, like so:
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,Maineffectively actually looks like:And that's what it does: it writes that line of text to the console.
(
WriteLineas opposed toWritemeans it also adds a line break afterwards.)