✅ static, what does it actually mean?

does someone have a good explanation to why and when to define a class or method as static? sometimes i get errors which i don’t understand why, but as soon as i define them as static it works perfectly fine. why?
6 Replies
Buddy
Buddy6mo ago
$static
MODiX
MODiX6mo ago
In C#, static allows you to have members (classes, methods, etc) that are not tied to any particular instance and are therefore always, globally, accessible. When applying static members, take the following considerations: • If there are to be multiple instances of a class, do not use static • If you need to track state, do not use static • If you are going to have multi threaded workflows, do not use static unless you're aware of the caveats static is best used for stateless methods, extension methods/classes and in areas where you understand the pros/cons. Read more here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static
Merineth 🇸🇪
Merineth 🇸🇪OP6mo ago
ahh so that’s why. i tried calling a method without an instance of an object? so when i define the method static it’s fine to call it even tho i don’t have an instance ?
this_is_pain
this_is_pain6mo ago
yes
Pobiega
Pobiega6mo ago
thats exactly what it means
Merineth 🇸🇪
Merineth 🇸🇪OP6mo ago
🫶🏻 thanks guys!

Did you find this page helpful?