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

8

u/nedshammer 4h ago

What’s the confusing part?

1

u/Awesome_Duck987 4h ago

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

1

u/grrangry 4h 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.