I have the following code snippet:
public static List<string> sqlData = new List<string>(); // // lots of code here // if (/* check here to see if the sqlData[whatever] index exists */) { sqlData.Insert(count2, sqlformatted); } else { sqlData.Insert(count2, sqlformatted + sqlData[count2]); }
I want to know how to check an index in sqlData to see if it exists before trying to insert something that contains.
If this is always positive, you can use this:
if (whatever < sqlData.Count) { ... }
Or, if everything can be negative, you also need to add a test:
if (whatever >= 0 && whatever < sqlData.Count) { ... }
Check the length by index:
sqlData.Count < count2
if(sqlData.Count > whatever ) { //index "whatever" exists string str = sqlData[whatever]; }
Source: https://habr.com/ru/post/1765173/More articles:Magento: the best way to avoid extension conflicts - magentoHow to call a function (not AJAX) after executing another function (not AJAX) in Dojo? - javascript-eventswhy does the contained div not recognize the height of the elements inside? - htmlHow to change layout settings while the program is running - androidImport Excel data into mysql database - mysqlHow to include php in heredoc variable? - phpHow to update $ PATH - pythonAndroid: adding a new view with XML layout file - androidHow to make sure a row cannot be accidentally deleted in SQL Server? - sql-serverIs there any way to avoid @SuppressWarnings in this code? - javaAll Articles