Question Index Out OfRange

metal_hard

New member
Joined
Nov 1, 2020
Messages
4
Programming Experience
Beginner
hello guys, help me solve the error, I'm a beginner, so I don’t know how to solve it, I looked many times I can’t understand what is wrong



1604951735203.png
 
In the future, please post your code as text in code tags. Also post the error or exception you are getting as text. It is hard to read screenshots on a small device. Furthermore, if we wanted to try replicating your problem, we can't just copy and paste your code. We are volunteers here. The bigger the hurdle you make for us to try to help you, the bigger the chance we will just move on and try to help someone else instead who made it easier for us to help.
 
Did you actually successfully recompile the code before running it? According to the debugger Auto variables pane, i is 10.

It is impossible of i to be 10 on that line of code because the outer for loop will break out when i is 10 because 10 < 10 is not true anymore on line 20. The only way I see that happening is that the code displayed does not match the code that is actually running.
 
in second loop u usede i++ on ending instead off j++


Fix your problem:
int n = 10;
int k = 10;
int[,] m = new int[n,k];

for (int i = 0; i<n; i++)
{
    for (int j = 0; j<k; j++)
    {
        m[i,j] = i+j;
        Console.WriteLine({0},m[i,j]);
    }
}
_ = Console.ReadKey();

if u not get the readkey value use "_ =" to discard this result to release memory. like an dispose a object or null a same.
 
Eagle eyes... Or someone who is actually read the code screenshot on a computer instead of a small phone like I was. This is why we ask people to actually post the code as text in code tags, not as screenshots.

As an aside, where did you read that _ = will discard or release the memory? I thought it was just to tell the compiler that you don't care about the return value. I didn't know that it will actually cause the return value to be automatically disposed or discarded.
 
Last edited:
Discards - C# Guide

You will have seen me using it occasionally in posts. A crude example :
C#:
        static void MethodA(string a, string b)
        {
            _ = string.Concat(a, b); /* Add var before _ and it no longer discard */
            Debug.WriteLine(_); /* _ Does not exist. */
        }
 
OP, there is a debugger tutorial in my signature. As a beginner, you should be able to debug your own code, before asking for help. Not trying to deny you the right to ask a question. But it's generally expected that you debug your own code so we don't have too. The error means you are trying to access a index in an array which is outside the range of that index set for that array. The error is rather self explanatory.
 
Yes, but where does it say there that objects will be disposed or the memory be freed up? All it says there is that memory might not even be allocated:
Because there is only a single discard variable, and that variable may not even be allocated storage, discards can reduce memory allocations.

I'm trying to follow up on:
use "_ =" to discard this result to release memory. like an dispose a object or null a same.

When I running the following code, I was expecting to see "I was disposed: Monkey1" on the console between "Starting Testing discard" and "Ending Testing discard", yet it does not show until later.
C#:
using System;

namespace SimpleCSConsole
{
    class CodeMonkey : IDisposable
    {
        string _name;
        bool disposedValue;

        public CodeMonkey(string name) => _name = name;

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                Console.WriteLine($"I was disposed: {_name}");

                if (disposing)
                {
                    _name = null;
                }

                disposedValue = true;
            }
        }

        ~CodeMonkey()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

        public void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }

    class Program
    {
        static void DoAction(string caption, Action action)
        {
            Console.WriteLine($">>> Starting {caption}");
            action();
            Console.WriteLine($"<<< Ending {caption}");
        }

        static void Main()
        {
            DoAction("Testing discard",
                () => {
                    _ = new CodeMonkey("Monkey1");
                });

            DoAction("Testing setting to null",
                () => {
                    var monkey = new CodeMonkey("Monkey2");
                    monkey = null;
                });

            DoAction("Testing explicit Dispose() call",
                () => {
                    var monkey = new CodeMonkey("Monkey3");
                    monkey.Dispose();
                    monkey = null;
                });

            DoAction("Testing using keyword",
                () => {
                    using (var monkey = new CodeMonkey("Monkey4"))
                        ; // do nothing
                });

            Console.WriteLine("Done.");
        }
    }
}

Output:
C#:
>>> Starting Testing discard
<<< Ending Testing discard
>>> Starting Testing setting to null
<<< Ending Testing setting to null
>>> Starting Testing explicit Dispose() call
I was disposed: Monkey3
<<< Ending Testing explicit Dispose() call
>>> Starting Testing using keyword
I was disposed: Monkey4
<<< Ending Testing using keyword
Done.
I was disposed: Monkey2
I was disposed: Monkey1
 
Back
Top Bottom