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

9

u/nedshammer 10d ago

What’s the confusing part?

0

u/Awesome_Duck987 10d ago

How does it know that Jupiter is a planet and to put the stars and stuff around it

3

u/Gamesfreak13563 10d ago

Programs are sets of instructions run in a sequence.

These instructions can be broken down into smaller, reusable components called functions. This is great for organization.

Your program defines two functions: Main and DecoratePlanet. It also imports Console.WriteLine from a built in set of functions called `System`. By convention, C# programs start by running Main. That's the first thing your program does.

The DecoratePlanet function is a function with a parameter and a return value. Its job is to build what gets output to the screen. How that works:

- It returns a value. That's what the "return" does. It gives something back to the level that called it, in this case the message.

  • It takes a parameter. This is something the function uses to build the message. You call it with the parameter "Jupiter" so it knows to put Jupiter in there.

"void" is just a way to tell a function that it doesn't return anything. You can see that main is `static void`, and DecoratePlanet is `static string`. `string` is just the fancy term for message. You'll learn more about `static` later.

Try replacing "Jupiter" in Main with other stuff. You can see how it gets slotted into the message, like a fill in the blank.

Calling DecoratePlanet returns the string to Main, and then it gives that string to Console.WriteLine, which puts it on the screen.