Variable initialization issue in switch statement
.net, c#, language-agnostic, switch-statement
Solution
Your code sample indicates that you will always make use of the `myControl` variable after the switch block. If that's the case, then you should either pre-initialize the variable, or add a `default` clause (as you mentioned).
If you are concerned that a new enumerated value might be introduced, then you could throw a meaningful exception in the `default` clause. That would safeguard you from getting a more ambiguous `NullReferenceException` when you try to dereference the variable later.
Problem
What I have is simple switch statement ``` Control myControl; switch(x) { case TabType.Edit: { myControl= ...; } case TabType.View: { myControl= ...; } } myPageView.Controls.Add(myControl); ``` In this situation compiler tells me that local variable myControl might not be initialized before accessing So, what is the best way to avoid this situation? One option is to initialize myControl before switch statement. But in this case I do one more unnecessary initalization. CASE 1: ``` Control myControl = null; switch(x) { case TabType.Edit: { myControl= ...; } case TabType.View: { myControl= ...; } } myPageView.Controls.Add(myControl); ``` Next option is to change second case with `default`. After that compiler will "understand" that myControl will be anyway initialized and will not throw exception. CASE 2: ``` Control myControl; switch(x) { case TabType.Edit: { myControl= ...; } default: { myControl= ...; } } myPageView.Controls.Add(myControl); ``` But this case doesn't look so good, because after adding some new properties to my enum it will do default for all other types(developer can easily forget to change code here or it can be not necessary to initialize myControl for other enum types). What is best approach in such situations?