Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8211897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:34:54+00:00 2026-06-07T10:34:54+00:00

This is the main class class Program { static void Main(string[] args) { MyArray

  • 0

This is the main class

 class Program
{
    static void Main(string[] args)
    {
        MyArray fruit = new MyArray(-2, 1);
        fruit[-2] = "Apple";
        fruit[-1] = "Orange";
        fruit[0] = "Banana";
        fruit[1] = "Blackcurrant";
        Console.WriteLine(fruit[-1]);       // Outputs "Orange"
        Console.WriteLine(fruit[0]);        // Outputs "Banana"
        Console.WriteLine(fruit[-1,0]);     // Output "O"
        Console.ReadLine();
    }
}

and here is the class for the indexer:

class MyArray
{
    int _lowerBound;
    int _upperBound;
    string[] _items;

    public MyArray(int lowerBound, int upperBound)
    {
        _lowerBound = lowerBound;
        _upperBound = upperBound;
        _items = new string[1 + upperBound - lowerBound];
    }

    public string this[int index]
    {
        get { return _items[index - _lowerBound]; }
        set { _items[index - _lowerBound] = value; }

    }

    public string this[int word, int position]
    {
        get { return _items[word - _lowerBound].Substring(position, 1); }
    }
}

So, multiDimensional indexer in defined in the ‘Class MyArray’

I am not able to understand how this is working when we pass ‘-1’ as the value of ‘word’ to the indexer. The value for ‘_lowerbound’ is ‘-2’. So it means, the value for the return should be _items[-1 – (-2)], which makes it _items[1].

But actually it is pointing to -1 index of fruits i.e. Orange.

Please clear my doubt.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:34:58+00:00Added an answer on June 7, 2026 at 10:34 am

    This is you private array _items:

       0     1     2     3
    ┌────┐┌────┐┌────┐┌────┐
    │ Ap ││ Or ││ Ba ││ Bl │          
    └────┘└────┘└────┘└────┘  
    

    This is how it appears using the indexer:

      -2    -1     0     1
    ┌────┐┌────┐┌────┐┌────┐
    │ Ap ││ Or ││ Ba ││ Bl │          
    └────┘└────┘└────┘└────┘  
    

    The value for _lowerbound is -2.

    So it means, the value for the return should be _items[-1 - (-2)], which makes it _items[1].

    That’s correct.

    But actually it is pointing to -1 index of fruits i.e. Orange.

    That’s wrong. It is pointing to _items[1], i.e. Orange.

    Indexers are a simple way to permit a class to be used like an array. Internally, you can manage the values presented in any fashion you wish.

    _items is a zero-based array with the same length as the created MyArray object.

    The indexer is just interpreting the index number supplied and map it against this underlying array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I write this: class Program { static void Main(string[] args) { throw new
First look at this code: class Program { static void Main(string[] args) { var
What is the workaround to this problem? class Program { static void Main(string[] args)
Consider this code snippet: class Program { static void Main(string[] args) { Console.WriteLine(Test().ToString()); }
I have this code: class Program { static void Main(string[] args) { Action whatToDo
lets say I have something like this: class Program { static void Main(string[] args)
What is wrong with this code-snippet? class Program { static void Main(string[] args) {
class Program { static void Main(string[] args) { var dictionary = new Dictionary<string, int>()
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine(new
This code: namespace ConsoleApplication3 { class Program { static void Main(string[] args) { var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.