✅ 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
$static
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/staticahh 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 ?
yes
thats exactly what it means
🫶🏻
thanks guys!