c# - Out of bounds exception on incrementing variable -


i've been while out of c# , mvc. , struggling following error, don't see it. have list of restrictions , want add keys string[].

int cntr = 0; //loop through restrictions , add array foreach (var restriction in this.admingrouprepository.context.adminrestrictions.tolist()) {     currentrestrictionkeys[cntr] = restriction.key;     cntr += 1; } 

this error on cntr += 1 line:

index outside bounds of array. 

i don't understand comes from, foreach breaks before cntr out of array's bounds right?

you have allocated little space currentrestrictionkeys. don't need preallocate @ all; can use trivial projection linq:

var currentrestrictionkeys = this.admingrouprepository.context.adminrestrictions                                  .select(r => r.key)                                  .toarray(); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -