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

Socket.connect doesn't throw exception in Android -

SPSS keyboard combination alters encoding -

iphone - How do I keep MDScrollView from truncating my row headers and making my cells look bad? -