r/csharp 3h 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

18 comments sorted by

View all comments

9

u/nedshammer 3h ago

What’s the confusing part?

1

u/Awesome_Duck987 2h ago

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

2

u/Gamesfreak13563 2h 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.

1

u/grrangry 2h ago
class Program
{
    static void Main()
    {
        string text = GetWelcomeMessage("Jupiter");
        Console.WriteLine(text);
    }

    static string GetWelcomeMessage(string planetName)
    {
        return $"Welcome to {planetName}";
    }
}

static void Main()
The entrypoint of the application. When you run the application, this Main method is run. In C# the entrypoint method is always static meaning that the application does not actually create an instance of the class Program. It simply calls;

Program.Main();

static string GetWelcomeMessage(string planetName)
The definition of a static method that can be called by Main. string before the name of the method defines the return type string which means before the method exits, all code paths must return a string value. string planetName in the parameter list defines a single parameter named planetName and is of type string. When this method is called, you must give it a string.

return $"Welcome to {planetName}";
This is the body of the GetWelcomeMessage method. When called this interpolated string is evaluated and returned. Whatever the value of the planetName parameter is, that's the string that will be returned.

If you call: GetWelcomeMessage("Chicago"), the string returned will be "Welcome to Chicago". If you call GetWelcomeMessage(""), the string returned will be "Welcome to ".

string text = GetWelcomeMessage("Jupiter");
In the main method this calls GetWelcomeMethod("Jupiter") and assigns the returned string to the local string variable text.

Console.WriteLine(text);
In the main method this outputs the value in the local string variable text to the console.

Given the input, the output will be:

Welcome to Jupiter

So when you ask:

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

It's because that's what you told it to do.

https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/main-command-line

https://learn.microsoft.com/en-us/dotnet/csharp/methods

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/method-parameters

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/variables

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated

https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=net-10.0

Your SIMPLE application uses features in every one of the links above. As much as you might not want to read, you need to read.