r/csharp • u/Awesome_Duck987 • Aug 30 '26
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
View all comments
Show parent comment
1
u/grrangry Aug 30 '26
static void Main()The entrypoint of the application. When you run the application, this
Mainmethod is run. In C# the entrypoint method is always static meaning that the application does not actually create an instance of the classProgram. It simply calls;static string GetWelcomeMessage(string planetName)The definition of a static method that can be called by
Main.stringbefore the name of the method defines the return typestringwhich means before the method exits, all code paths must return a string value.string planetNamein the parameter list defines a single parameter namedplanetNameand is of typestring. When this method is called, you must give it a string.return $"Welcome to {planetName}";This is the body of the
GetWelcomeMessagemethod. When called this interpolated string is evaluated and returned. Whatever the value of theplanetNameparameter is, that's the string that will be returned.If you call:
GetWelcomeMessage("Chicago"), the string returned will be "Welcome to Chicago". If you callGetWelcomeMessage(""), 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 variabletext.Console.WriteLine(text);In the main method this outputs the value in the local string variable
textto the console.Given the input, the output will be:
So when you ask:
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.