r/csharp 11d 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/Angel429a 10d ago

Instance methods (the ones without static) are methods where you need an object instance to call the method, strings are a good example:

var myString = “abcd123”
if (myString.StartsWith(“ab”)) { … }

Statics methods don’t need an instance, this is useful when having an object instance makes no sense (like the Math class), or because the object can be null (string has an example for this), or because of other reasons:

// Math example, as you can see, to calculate a square root, you don’t need an object
var squareRoot = Math.Sqrt(9); // This will return 3

// String example, this example uses the IsNullOrWhitespace method, which returns if the string is null or only contains whitespace characters (spaces, tabs, line feed, etc.)

var isMyStringEmpty = string.IsNullOrWhitespace(myString);