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 9214861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:04:05+00:00 2026-06-18T02:04:05+00:00

I am trying to create: Sparse Array with arbitrary indexing and have have Sparse

  • 0

I am trying to create:

  • Sparse Array with arbitrary indexing and
  • have have Sparse Array inherit IEnumerable.

So far I have the indexing… the IEnumerable part I don’t know what to do.

class SparseArray : IEnumerable
{
    private Dictionary<object[], object> Items = new Dictionary<object[], object>();
    private int Counter = -1;
    public object this[params object[] Key]
    {
        set { Items.Add(Key, value); }
        get { object B; bool Tau = Items.TryGetValue(Key, out B); return B; }
    }
    public IEnumerator GetEnumerable()
    {
        ///
    }
}
class Program
{
    static void Main(string[] args)
    {
        uint W = 5;
        Potato P = new Potato(W, "Spudz");
        Potato Q = new Potato(W+2, "Super Spudz");
        SparseArray Pile = new SparseArray();
        Pile[1,2,3,4,5,Q] = "String";
        Pile["String",P] = "Strung";
        Pile[1.2030,"pi"] = true;
        foreach ( object Val in Pile)
        {
            Console.WriteLine(Val);
        }
        Console.ReadKey();
    }
}
struct Potato
{
    private string _Name;
    private uint _Weight;
    public string Name
    {   set { _Name = value; }
        get { return _Name; }
    }
    public uint Weight
    {
        set { _Weight = value; }
        get { return _Weight; }
    }
    public Potato(uint weight, string name)
    {
        _Weight = weight;
        _Name = name;
    }
}

How can I get the Sparse Array object to loop through its dictionary within the foreach loop?

EDIT UPDATE

I have made slight revision to the code thanks to the input from everybody:

public class ObjectArrayComparer : IEqualityComparer<object[]>
{
    // Determines whether x and y are equal or not
    public bool Equals(object[] x, object[] y)
    {
        return object.ReferenceEquals(x, y) // Returns true if they are the same array instance
            || (x != null && y != null && x.SequenceEqual(y));  // Returns true if x and y are not null and have the same elements (order dependent)
    }

    // Function that allow to fastly determine if an element is in a set of not.        
    // This function must have the following property:
    //   x.Equals(y) implies GetHashCode(x) == GetHashCode(y)
    public int GetHashCode(object[] obj)
    {
        if (obj == null)
            return 0;

        // Unchecked sum of the Hash Codes of all elements in obj
        return unchecked(obj.Select(o => o != null ? o.GetHashCode() : 0).Aggregate(0, (a, b) => a + b));
    }
}
class SparseArray : IEnumerable
{
    private Dictionary<object[], object> Items = new Dictionary<object[], object>(new ObjectArrayComparer());
    private int Counter = -1;
    public object this[params object[] Key]
    {
        set { Items.Add(Key, value); }
        get { 
                object B;
                if (Items.TryGetValue(Key, out B) == true)
                {
                    return Items[Key];
                }
                else
                {
                    //In computer science, a sparse array is an array in which most of the elements have the same value (known as the default value—usually 0 or null).
                    //So If the key does not exist, return null.   
                    return null;
                }

            }
        //get { object B; bool Tau = Items.TryGetValue(Key, out B); return B; }
    }
    public IEnumerator GetEnumerator()
    {
        return Items.Values.GetEnumerator();
    }
}
class Program
{
    static void Main(string[] args)
    {
        uint W = 5;
        Potato P = new Potato(W, "Spudz");
        Potato Q = new Potato(W+2, "Super Spudz");
        SparseArray Pile = new SparseArray();
        Pile[1,2,3,4,5,Q] = "String";
        Pile["String",P] = "Strung";
        Pile[1.2030,"pi"] = true;
        foreach ( object Val in Pile)
        {
            Console.WriteLine(Val);
        }
        Console.WriteLine(Pile[1.2030, "pi"]);
        Console.WriteLine(Pile["String", P]);
        Console.WriteLine(Pile["String", P]);
        Console.WriteLine(Pile["String", Q]);
        Console.ReadKey();
    }
}
struct Potato
{
    private string _Name;
    private uint _Weight;
    public string Name
    {   set { _Name = value; }
        get { return _Name; }
    }
    public uint Weight
    {
        set { _Weight = value; }
        get { return _Weight; }
    }
    public Potato(uint weight, string name)
    {
        _Weight = weight;
        _Name = name;
    }
}
  • 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-18T02:04:06+00:00Added an answer on June 18, 2026 at 2:04 am

    The easiest way is to do:

    public IEnumerator GetEnumerator()
    {
        return Items.GetEnumerator();
    }
    

    Or, if you only want the values:

    public IEnumerator GetEnumerator()
    {
        return Items.Values.GetEnumerator();
    }
    

    Don’t forget to use a custom comparer for your dictionary:

     private Dictionary<object[], object> Items = new Dictionary<object[], object>(new ObjectArrayComparer());
    

    With:

    public class ObjectArrayComparer : IEqualityComparer<object[]>
    {
        // Determines whether x and y are equal or not
        public bool Equals(object[] x, object[] y)
        {
            return object.ReferenceEquals(x, y) // Returns true if they are the same array instance
                || (x != null && y != null && x.SequenceEqual(y));  // Returns true if x and y are not null and have the same elements (order dependent)
        }
    
        // Function that allow to fastly determine if an element is in a set of not.        
        // This function must have the following property:
        //   x.Equals(y) implies GetHashCode(x) == GetHashCode(y)
        public int GetHashCode(object[] obj)
        {
            if (obj == null)
                return 0;
    
            // Unchecked sum of the Hash Codes of all elements in obj
            return unchecked(obj.Select(o => o != null ? o.GetHashCode() : 0).Aggregate(0, (a, b) => a + b));
        }
    }
    

    Why?

    Try this:

    var a = new int[] { 0 };
    var b = new int[] { 0 };
    Console.WriteLine(a == b);  // Returns false
    

    Because the default comparer on an array is a reference comparison. As a and b are two instances of int[], they have not the same reference.

    To change this behavior, you have to specify in the dictionary, how you want to compare the object[].

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

Sidebar

Related Questions

I'm trying to create a sparse vector class in C++, like so: template<typename V,
I'm trying create new object from a module class in VBA, and I have
I trying create a class derivated from System.Web.UI.Page and in override Render i set
I am trying to create variable number of sparse matrices. At first the best
I'm trying to create a two dimensional linked list to hold a sparse matrix
I am trying to create a sparse matrix with the data from database. Here
I am trying to create a sparse matrix which has a 2D pattern run
I'm trying to create a fly-by-seat-of-pants sparse vector library in scala and I'm hitting
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can

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.