✅ Static keyword
Hello guys, I have a small question. Here, notice we use the
static
keyword with the array Country and City
. My question is, static keyword can only be used in the class where Country and City exist? That is, say we have class City and Country, in both classes we would have the declaration of an array of their respective type with static and readonly keyword? We can't use static keyword for a variable in our Program.cs for example?6 Replies
I'm not entirely sure what you are asking
The static keyword basically means "single instance"
You can store this data this way, sure. I'd say it would be better to store it in a file or database though
It is not related to your class in any way
You can use
static
in the Program
class. If you use top-level statements then no, since that behaves like a method body
Local variables cannot be staticIf you make it static in this case, your array will not be created with each instance of the containing class you create. It would be better for performance, but that also assumes the array is not mutated in any way. Otherwise you risk concurrency or undefined behaviour
oh ok I see, didn't know why I got confused with the
static
keyword but it's clearer now, thanks !