المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : A matter of style : Switch or ?: [modified]



C# Programming
10-21-2009, 12:40 AM
Suddenly not sure which of the following constructs I should go with...

I really like the compactness of B, but a bit unsure how my fellow coders would react to seeing the construct.

I can't see how there should be any performance differences (if you know differently - Do tell)

A:

int i;

switch (ID) {
case "a":
i = 1;
break;
case "b":
i= 2;
break;
case "c":
i = 3;
break;
default:
i = 0;
break;
}


B:

int i;

i = ID == "a" ? 1 :
ID == "b" ? 2 :
ID == "c" ? 3 :
0;


What would you use (and why)?

modified on Tuesday, October 20, 2009 6:46 AM