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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:49:01+00:00 2026-06-08T21:49:01+00:00

I am required to key a dictionary based on values of a datastructure. I’m

  • 0

I am required to key a dictionary based on values of a datastructure. I’m wondering what would be the optimal way to create this key?

The datastructure has 3 values: two strings and a datetime. The three of these values combined represents a “unique” key for my dictionary.

public class RouteIdentity
{
     public string RouteId {get;set;}
     public string RegionId {get;set;}
     public DateTime RouteDate {get;set;}
}

One solution that comes to mind is to add a property to RouteIdentity (called Key perhaps?) that returns some representation of the 3 unique values. The type of Key would be the type of the key value of the dictionary. Key could be a string value that simply concatenates the various properties, but this seems terribly inefficient. I suppose if there were a way to implement a fast hashing function to return a different type that might also work.

Another possibility to is override the Equals operator for RouteIdentity. I’m thinking this might be a better approach, but I’m unsure of how to override the GetHashCode() function for such a purpose.

Can anyone shed some light on what the optimal approach would be for this case? If you feel that it would be best to use operator overloading, could you please provide some guidance as to how to implement it properly?

Thanks in advance.

  • 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-08T21:49:05+00:00Added an answer on June 8, 2026 at 9:49 pm

    Implement Equals() and GetHashCode(), ..

    public class RouteIdentity
    {
        public string RouteId { get; set; }
        public string RegionId { get; set; }
        public DateTime RouteDate { get; set; }
    
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }
            if (ReferenceEquals(this, obj))
            {
                return true;
            }
            if (obj.GetType() != typeof(RouteIdentity))
            {
                return false;
            }
    
            RouteIdentity other = (RouteIdentity) obj;
    
            return Equals(other.RouteId, RouteId) && 
                   Equals(other.RegionId, RegionId) && 
                   other.RouteDate.Equals(RouteDate);
        }
    
        public override int GetHashCode()
        {
            unchecked
            {
                int result = (RouteId != null ? RouteId.GetHashCode() : 0);
                result = (result * 397) ^ (RegionId != null ? RegionId.GetHashCode() : 0);
                result = (result * 397) ^ RouteDate.GetHashCode();
                return result;
            }
        }
    }
    

    … and use new Dictionary<RouteIdentity, TValue>(), which internally will instantiate EqualityComparer<RouteIdentity>.Default, which uses these 2 methods to compare your RouteIdentity instances.

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

Sidebar

Related Questions

Dictionary<string,string> l_dictData = new Dictionary<string,string> the above dictionary contains data like this: (1)key =
I have two dictionary objects, connections and network. This can be visualized as a
In Java, I've these key/value pairs passed by post (for example): form_id=undefined frmb[0][cssClass]=input_text frmb[0][required]=checked
In one of my models, I want a foreign key object to be required
Is there a way to require an API key in the URL / or
i write this in .emacs (require 'dired-x) (global-set-key (kbd C-x C-j) 'dired-jump) but when
I need to assign values from a small dictionary collection (10 items) to a
I would like to observe changes to settings values in [NSUserDefaults standardDefaults]. I am
In an ASP.NET 2.0 site on IIS6 I would like to store Key /
I have a dictionary like class that I use to store some values as

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.