C# Break out of foreach loop after X number of items

int processed = 0; foreach(ListViewItem lvi in listView.Items) { //do stuff if (++processed == 50) break; } or use LINQ foreach( ListViewItem lvi in listView.Items.Cast<ListViewItem>().Take(50)) { //do stuff } or just use a regular for loop (as suggested by @sgriffinusa and @Eric J.) for(int i = 0; i < 50 && i < listView.Items.Count; i++) … Read more

Combining foreach and using

foreach (ManagementObject result in results) using(result) { //code here } It’s not normally good practice to assign the variable outside the using block because the resource would be disposed but could stay in scope. It would, however, result in clearer code here because you can nested the using statement against the foreach. EDIT: As pointed … Read more

Loop (for each) over an array in JavaScript

TL;DR Your best bets are usually a for-of loop (ES2015+ only; spec | MDN) – simple and async-friendly for (const element of theArray) { // …use `element`… } forEach (ES5+ only; spec | MDN) (or its relatives some and such) – not async-friendly (but see details) theArray.forEach(element => { // …use `element`… }); a simple … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)