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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:17:52+00:00 2026-05-23T10:17:52+00:00

My program has a grid of cells and I want to be able to

  • 0

My program has a grid of cells and I want to be able to efficiently query either by row or by column number. What kind of structure should I use to do this?

For example I’d like to have the following methods:

CellsCollection.GetCell(Int32 row, Int32 column)
CellsCollection.GetAllCellsInRow(Int32 row)
CellsCollection.GetAllCellsInColumn(Int32 column)

My first try was to create a struct with two fields (row and column) and then a dictionary with a composite key of the struct: Dictionary<struct, cell>.

CellsCollection.GetCell(Int32 row, Int32 column) is no problem because I’d be querying the dictionary by the composite key.

The other two (get cells in row/column) present the problem because if I do:

dictionary.Where(keyPair=>keyPair.Key.Row == row).Select(keyPair=>keyPair.Values.Cell)

Then the dictionary key becomes moot and the program has to go through every key in the dictionary.

I thought of a nested dictionary (the outer one with a row key and the inner one with a column key) but I that would only help if I’m querying by row, not by column.

How would you overcome this?

  • 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-23T10:17:53+00:00Added an answer on May 23, 2026 at 10:17 am

    A dictionary is great if you have gaps in your indexes. If you have a grid of cells then I’m guessing then that is not the case (unless you have lots of empty cells).

    So, why not have an 2-dimensional array? e.g.

    int[,] cells = new int[maxRow,maxColumn];
    

    That way if you want to query a specific cell you just do

    int cellValue = cells[row,column]
    

    or

    public int GetCell(Int32 row, Int32 column)
    {
        return cells[row, column]
    }
    

    If you want to get everything in a row:

    for(int col = 0; col < maxColumn; col++)
        int cellValue = cells[row, col];
    

    or

     public IEnumerable<int> GetAllCellsInRow(Int32 row)
     {
        for(int col = 0; col < maxColumn; col++)
            yeldReturn cells[row, col];
     }
    

    And for everything in a column similarly

    for(int row = 0; row < maxRow; row++)
        int cellValue = cells[row, column];
    

    or

    public IEnumerable<int> GetAllCellsInColumn(Int32 column)
    {
        for(int row = 0; row < maxRow; row++)
            yield return cells[row, column];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The data model in my program has a number of discrete states, but I
I want to add a header row grouping the column headers. Departure Arrival <--
In windows form application our data grid view has lots of event like row
I am making a complex tic tac toe program that has a variable grid
I have a class called Program which hold a grid. It has two methods
My program has two kernels and the second kernel should use the already uploaded
My program has to use certain files that reside in another directory. Right now
My program has two threads: Main execution thread that handles user input and queues
My program has one dialog and two sockets. Both sockets are derived from CAsyncSocket,
My program has the following class definition: public sealed class Subscriber { private subscription;

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.