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

  • Home
  • SEARCH
  • 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 6336569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:04:56+00:00 2026-05-24T19:04:56+00:00

I want to create class representing n-dimensional array, but where is a commutative access

  • 0

I want to create class representing n-dimensional array, but where is a commutative access to its elements. e.g: a[new[] {4, 7, 55}] == a[new[] {55, 4, 7}]

I write this code, where I implement interface IEqualityComparer in order to compare keys (which are arrays) by their real content, but not refs.

using System;
using System.Collections.Generic;
using System.Linq;

class NArray
{
    public int this[int[] x]
    {
        get
        {
            Array.Sort(x);
            return array[x];
        }
        set
        {
            Array.Sort(x);
            array[x] = value;
        }
    }

    public void Remove(int[] x)
    {
        Array.Sort(x);
        array.Remove(x);
    }

    Dictionary<int[], int> array = new Dictionary<int[], int>(new ArrCmpr());
}

class ArrCmpr : IEqualityComparer<int[]>
{
    public bool Equals(int[] a, int[] b)
    {
        return a.Length == b.Length && Enumerable.Range(0, a.Length).All(i => a[i] == b[i]);
    }

    public int GetHashCode(int[] a)
    {
        return a.GetHashCode();
    }
}

But when I start to use this class I encounter an exception: “System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.” This exception occurs in next both cases when I try to output the element to console:

NArray a = new NArray();
a[new[] { 1, 3, 2 }] = 4;

Console.WriteLine(a[new[] { 3, 2, 1 }]); //error

 

NArray b = new NArray();
b[new[] { 1, 2, 3 }] = 4;
Console.WriteLine(b[new[] { 1, 2, 3 }]);    //error

So what is the cause of that problem and how could I fix it?

  • 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-05-24T19:04:56+00:00Added an answer on May 24, 2026 at 7:04 pm

    That’s because your implementation of GetHashCode is incorrect: two different arrays with the same items in the same order usually won’t have the same hashcode (because the values are not taken into account), so Equals is never called.

    You need an implementation of GetHashCode that takes the values in the array into account:

    class ArrCmpr : IEqualityComparer<int[]>
    {
        public bool Equals(int[] a, int[] b)
        {
            return a.SequenceEqual(b);
        }
    
        public int GetHashCode(int[] a)
        {
            return a.Aggregate(0, (acc, i) => unchecked(acc * 457 + i * 389));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a new object using Class::DBI. One of the fields of
I want to create a similar behavior to the data reader class but for
I want to create new class MapActivity from Eclipse. I go File-> New ->
I want to create a class that is usable without being instanced, but that
I want to create an abstract class in C#. Currently, I have this class
I want to create a Scala class where one of its var is read-only
I want to create a class that has several public variables and methods,but behaves
I want to create a static class in PHP and have it behave like
I want to create an alias for a class name. The following syntax would
All, I want to create a function that takes a symbol representing a java

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.