❔ ✅ Static vs. non-Static (?)
Hello hello (: Im a very beginner, and trying to learn about delegates. But i stumbled upon the topic of Static/NonStatic - i've read about what it does; Static members dont operate on the instance of a class, but the class itself, whereas non-static members operate on the instance.
(A memeber that keeps track of all the instances of a class could be static)
But i'm having much trouble deciding what should be static or not, even with the information. How do i decide it? I have this example, where i feel a static-method would be more flexible in the given context - but i dont know if i should?
Do i decide what is static or not or do some rules i may not understand/know yet decide what should be static or not?
Thanks very much in advance 🙂
3 Replies
public void NotifySMS(Book book)
because this is an instance method, you don't need to pass state into it, you can just declare it as public void NotifySMS()
and use the state of the object itself
so it wouldn't be book.Title
it'd just be Title
a good general rule of thumb is to avoid static where possible, it's not a perfect rule because there are absolutely valid reasons to use static, but approaching problems with static as a "last resort" means you avoid the trap of the static state
if you use static too freely, then you start running into problems where B needs to be static because A is static, and then C needs to be static because B is static, etc.
obviously in your case you'd need to modify your delegate signature slightly, rarely if ever declare raw delegates anymore
Action
and Func
are 👌Ohh! Thank you for the answer 😄 So there are multiple things to unpack here, and i'tll take me a minute but very cool! Also more simple 😄 yaay
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.