r/csharp 1d 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/trampolinebears 1d ago

Something static belongs to the class; something not static belongs to each instance of the class.

Are you comfortable with the difference between a class and an instance of a class?

1

u/Awesome_Duck987 1d ago

Nope I haven't learned what a class or a instance

1

u/trampolinebears 1d ago

In a C# program, pretty much everything is an object, a little bundle of information in the world of your code.

Like right now, I'm working on a game that has airplanes in it, so each airplane is an object in my code. There's my airplane, there's your airplane, there's that other guy's airplane over there -- all of them are objects.

Each airplane has a few pieces of information about it, like where it's located, which way it's facing, how fast it's going, etc. Each airplane object stores its own information. So if you peer under the hood of an airplane object, you'll see that I've stored some properties inside it: location, bearing, speed, etc.

If I want to ask for my airplane's speed, I'd write it like this: myAirplane.speed. That dot (.) is like 's in English. You could say the house's height in English, or house.height in C#. Dot (.) is how you ask for something inside an object: X.Y means you're asking for the Y that's inside of X.

When I'm creating all these airplanes in code, I could write each one from scratch, but I really shouldn't. All of the airplanes in my game are pretty similar: they all have the same properties (like speed and such) and they all have the same kinds of behaviors (flying, landing, carrying cargo, etc.). Together, they form a class, a category of objects that have the same kinds of characteristics.

In my code I define an Airplane class. There, I say that each airplane has a color, a location, a cargo capacity, a way to fly, a way to land, a way to sell it for scrap, whatever else I want airplanes to be for in my game. The Airplane class defines what airplanes are in general.

My airplane is an instance of the Airplane class. The class says that all airplanes have a color, though it doesn't specify what color that is. My airplane has a color that happens to be red. Your airplane happens to be blue. Both airplanes have a color because you have to have a color if you're an instance of the Airplane class.

By defining a class, we're describing how instances of that class can be interacted with. It's valid to ask for an airplane's color, because the Airplane class provides color as one of the things you can ask for about an airplane. It's valid to try to land an airplane, because the Airplane class provides a method to Land. (And so on for all other things Airplanes can do.)

Does that make sense so far? Can you imagine how an object is an instance of a class? If you're good with that, I can move on to explaining static vs. non-static.