Modify Collection while Iterating C#
The general rule is that you can’t modify a collection/array/list while iterating over it.
If you are trying to modify loop while iterating you will get following error message
List strList = new List();
strList.Add(“1”);
strList.Add(“2”);
strList.Add(“3”);
strList.Add(“4”);foreach(string str in strList)
{
strList.Add(“test”);
}Error:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.List`1.Enumerator.MoveNextRare() at System.Collections.Generic.List`1.Enumerator.MoveNext() at Rextester.Program.Main(String[] args)
Here is the simple solution
int listLength = strList.Count();
for(int i=0; i<listLength; i++)
{//Add New Element
if(i == 0)//Some codition when you want to add new element to list/array
{
listLength++;
strList.Add(“5”);
}Console.WriteLine(strList[i]);
}Hope it helps !
Leave a Reply Cancel reply
Categories
- AngularJs (13)
- ASP.NET (104)
- C Programming (1)
- Code Project (2)
- CodeProject (1)
- Entity Framework (6)
- Ext.NEt (1)
- HTML5 (1)
- Interview .NET (2)
- Introduction (1)
- JQuery (1)
- Linq (1)
- Microsoft office (2)
- MOSS-(Sharepoint 2010) (2)
- MVC (5)
- Outlook (1)
- Sql Server (30)
- SSRS (5)
- TFS (1)
- Tips & Tricks (16)
- Twitter bootstrap (1)
- VB.NET (1)
- Visual Studio 2012/2013 (1)
- Web Services (1)
- Windows Workflow (1)
Top Posts & Pages
- How to Collapse all #regions only(!) in C# (Visual Studio)
- Error :(System.Web.UI.HtmlControls.HtmlIframe) is not compatible with the type of control (System.Web.UI.HtmlControls.HtmlGenericControl) in VS2017
- Disable click outside of bootstrap model area to close modal
- MYSQL : How to skip first 10 records from a SELECT query and take next 10?
- Modify Collection while Iterating C#
- Kendo grid error : kendo.all.min.js:12 Uncaught TypeError: e.slice is not a function
- Updatepanel triggers another updatepanel
- UpdatePanel and triggers from a repeater control
- Send Email from a Static HTML
- hide img tag if src is empty but without javascript/jQuery or css3
Recent Posts
- How to compile .NET Core Console Application as .exe File?
- Define Composite Key in Model.(Core, EF Code First)
- How to remove x from Internet Explorer ?
- The client and server cannot communicate, because they do not possess a common algorithm
- How to check if all items are the same in a list C#
- How to Collapse all #regions only(!) in C# (Visual Studio)
- Issue: Password field returned empty in edit mode
- Load XML from URL giving an error (The remote server returned an error: (401) Unauthorized.)
- How to use Variable in MySQL Like Clause ?
- Error :(System.Web.UI.HtmlControls.HtmlIframe) is not compatible with the type of control (System.Web.UI.HtmlControls.HtmlGenericControl) in VS2017
Blog Stats
- 157,064 hits
Rahul Bhatia
Comments
aishwarya on remove pie chart slices labels… | |
Chakib on UpdatePanel and triggers from… | |
Mehdi Bennis on AngularJS With Asp.net Web API… | |
hnc24 on Send Email from a Static … | |
Rob on Send Email from a Static … |
Profile
Blog Stats
- 157,064 hits
Use more button for sharing this post to WhatsApp and Skype