Index was outside the bounds of the array error. C# [duplicate] - c#

This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 6 years ago.
I am trying to create a two dimensional numeric array in c#. In which there are the following simple methods: generates the array, shows the array results in the console and finally counts and shows the sum of all of the rows and columns in it.
But when i am trying to get the sums of rows and columns i get this error:
The number of rows and columns are specified in the first row of the data.txt file.(5 rows and 7 columns)
The data.txt file looks like this:
This is my first post in SOF so i apologize for any mistakes.
thanks in advance.

Assuming column count != row count - it should be
result = new int[columns] instead of result = new int[rows]
since you initialize result with result = new int[rows] and i is iterating the columns

Related

index out of range exception occurred [duplicate]

This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 1 year ago.
do not be tired
I defined a 1000-element presentation in the Windows application form space and used it in various areas of the program, but unfortunately I encounter the following error. Thank you for your help:
enter image description here
I have defined this variable as follows :
enter image description here
Indexes in arrays start with 0. So array with 1000 elements will have indexes from 0 to 999. It throws at the 1000th index. As a solution, you can change your loop to have following condition: i < 1000 instead.

I saw this in code to do with lists and I'm not sure what it does [duplicate]

This question already has answers here:
What does it mean when there is a comma inside an array declaration? e.g. float[,]
(4 answers)
Closed 2 years ago.
So I'm trying to learn from other peoples code and I saw this at the end of a list and could not find it anywhere I looked
List<IMyShipMergeBlock>[,] merges = new List<IMyShipMergeBlock>[3, 2];
What is [,] and [3,2] called and could you point me to where I could learn more about it thanks
its a multidimensional array of List<IMyShipMergeBlocks>
The [3,2] is the initialiser for the array
so imagine a 3x2 grid and in each square there is a space for a List<IMyShipMergeBlocks>
EDIT: And important to note: You would then need to initialise each list separately as with the above you have an array of nulls to start with

C# list always shows 1 element even though the list is empty [duplicate]

This question already has answers here:
c# split string and remove empty string
(6 answers)
C# list count always returns 1 even when list is empty
(9 answers)
Closed 2 years ago.
I have a variable which stores some comma separated values as shown below
string Names ="Max,Amy,Anderson"
I have written a code which addds each of these comma seperated values to a List as shown below
List<string> listOfItens= new List<string>();
listOfItens = Names.Split(',').ToList();
Something I have noticed is that in a scenario where string Names =""; is empty the listOfItens will still have a count of 1 with the value "".
Any reason why it happens like that and is there a way I can get the listOfItens .Any() = null or listOfItens .count()==0
Came across this on SO but it didnt quite answer my question C# list count always returns 1 even when list is empty

Query a list and select top 10 values [duplicate]

This question already has answers here:
How to get first N elements of a list in C#?
(7 answers)
Closed 4 years ago.
I have a list of objects in which each object has a property called "Frequency" and I want to be able to pick the top 10 objects that have the highest frequencies.
I saw some solutions that are kind of similar to what I am looking to solve using LINQ so any help is appreciated.
You can order the list by descending Frequency and then take the first 10 like this:
var top10 = objectList.OrderByDescending(o => o.Frequency).Take(10);

Select 50 to 100 rows from datatable [duplicate]

This question already has answers here:
how to get second record in linq to sql
(9 answers)
Closed 8 years ago.
I need to select rows 50 to 100 from a datatable. I have tried for the first 50 rows using this code:
dt.Rows.Cast<System.Data.DataRow>().Take(50)
Now I need rows 50 to 100 from the datatable. How do I do this?
Use Skip method
dt.Rows.Cast<System.Data.DataRow>().Skip(50).Take(50)

Categories