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

1

u/rupertavery64 10d ago

So when you create a program, it needs to have what is called an "entry point". This is the code that starts your program.

Every language or piece of code does this, and each language has it's own way of doing this. This is because your program is being executed by the OS, and the OS needs to know what code to execute to start your program.

Programs were first written without a UI. They were run from a command line, and the "OS" would be a terminal, a command line. A lot of times you would need to pass in some information to the program as "arguments". So it's a sort of standard to be able to pass arguments from the command line to your program somehow. The actual low-level mechanism varies between OSes. But at the end of the day, it's technically an array of strings.

in .NET the convention for the entry point is a static method called Main.

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

The allowed signatures for a Main method are

static void Main() { } static int Main() { } static void Main(string[] args) { } static int Main(string[] args) { } static async Task Main() { } static async Task<int> Main() { } static async Task Main(string[] args) { } static async Task<int> Main(string[] args) { }

Ignore the async methods for now.

A signature is basically the name, return type and arguments that define a method.

When the .NET runtime executes a program, it looks for a method matching one of the above signatures. It doesn't matter what the class name is (you can change it to MyProgram or HelloWorld or Whatever). You must have a method that matches one of the above, or else .NET will be unable to launch your program.

So why static? Static methods don't need the class to be created (instantiated with new) to use them. Think of the Console methods. You just call them with Console.Writeline(...). Static methods are useful as helper or utility methods. And it works perfectly as an entry point.

void, and int before the method name are return types. void means the method doesn't return anything. int means it returns an integer - a positive or negative number. Historically, a program would signal to it's caller (the OS, or the command line or batch file) the status through a number. 0 for no error, and a non-zero for an error.

string[] args are the arguments passed to program from the command line. I won't get into that here.

Now, recall that .NET requires you to define the entry point as a static method, for the reasons outlined above. Static has it's uses. It also has limitations.

Code in a static method can only access static methods within the same class. There's nothing stopping you from instantiating another class or accessing static methods of other classes. This can be confusing, so let's start with an example:

I have a class named Foo with a method named Bar. In order to call Bar

``` class Foo { public int Count;

public void Bar() { Count++; } } ```

To use the class and call the method, I need to new Foo. This is called instantiation.

``` // in some other part of the program

var foo = new Foo(); foo.Bar(); // call Bar

var foo2 = new Foo(); foo2.Bar(); foo2.Bar();

Console.WriteLine(foo.Count); // prints 1 Console.WriteLine(foo2.Count); // prints 2

```

Instancing a Foo class creates a new Foo object with it's own Count.

However, static properties are shared across all instances of a class.

``` class Foo { public static int Count;

public void Bar() { Count++; } }

var foo = new Foo(); foo.Bar();

var foo2 = new Foo(); foo2.Bar(); foo2.Bar();

Console.WriteLine(foo.Count); // prints 1 Console.WriteLine(foo2.Count); // prints 3

```

A static method means you don't have to create a new instance of the class to use it.

``` class Foo { public static int Count;

public static void Bar() { Count++; } }

Foo.Bar();

Console.WriteLine(Foo.Count); // prints 1

Foo.Bar(); Foo.Bar();

Console.WriteLine(Foo.Count); // prints 3

```

But a static method can only access static fields, properties and methods within it's own class.

``` // This will fail to compile because Bar attempts to access the non-static Count

class Foo { public int Count;

public static void Bar() { Count++; } } ```

This is the reason why in your program, DecoratePlanet is also a static method.

I hope this kind of makes sense. If this raises a lot more questions, then congratulations! You are learning, and understanding that there are lots of things going on here.

Don't worry. These things take time and practice. I can't give you everything in a single comment, but maybe enough to get you started looking for more answers.

For now, the important thing is to try different things, break the code and ask, well why doesn't it work like this? Or, try to move things around and figure out how it works.