✅ Possible bug in ternary operator when incrementing in razorpages?

inside some .cshtml: @{ int a; int k; if(Model.SomeArray != null) { a = Model.SomeArray[0].SomeIndex; } for(int i=0; i<Model.SomeOtherArray.Length; i++) { if(i==a) { //do somework k = (k+1 < Model.SomeArray.Length) ? k++ : k; } } } correct me if im wrong but k should increment here and then be saved? or is the ternary operator scope localized and the parameter get incremented in another scope and the obj ref then dropped, meaning that the work is lost? in razor pages k++, or even k=k++; doesnt update the object. k=k+1 works though. This should be a bug right?
4 Replies
Kouhai /人◕ ‿‿ ◕人\
It's not a bug, if you want to increment and assign you would do k=++k Though why not just do
if (k+1 < Model.SomeArray.Length) {
k++;
}
if (k+1 < Model.SomeArray.Length) {
k++;
}
nightooi /s I'm rarely serious
ahaha, got bit in the ass by the assignment ordering harold . I don't want too keep nesting, this is just the start of the component and its already starting to become a nightmare, thank you! c:
Kouhai /人◕ ‿‿ ◕人\
It's not assignment ordering, k++ is postfix increment it returns the original value before the increment ThumbsUp essentially
int temp = k;
k++;
k = temp;
int temp = k;
k++;
k = temp;
nightooi /s I'm rarely serious
right post and prefix, never learnt the terms correctly. thank you
Want results from more Discord servers?
Add your server
More Posts