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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:23:27+00:00 2026-05-15T08:23:27+00:00

So, I need to create a struct in C# that will act as a

  • 0

So, I need to create a struct in C# that will act as a key into a (quite large) dictionary, will look like this:

private readonly IDictionary<KeyStruct, string> m_Invitations;

Problem is, I REALLY need a struct to use as a key, because it is only possible to identify entries via two separate data items, where one of them can be a null (not only empty!) string.

What will I need to implement on the struct? How would you go about creating the hash? Would a hash collision (occassional) hurt the performance heavily or would that be negligible?

I’m asking because this is “inner loop” code.

  • 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-15T08:23:28+00:00Added an answer on May 15, 2026 at 8:23 am

    If you have resharper, you can generate these method with Alt-Ins -> Equality members.

    Here is the generated code for you KeyStruct:

    public struct KeyStruct : IEquatable<KeyStruct>
    {
        public string Value1 { get; private set; }
        public long Value2 { get; private set; }
    
        public KeyStruct(string value1, long value2)
            : this()
        {
            Value1 = value1;
            Value2 = value2;
        }
    
        public bool Equals(KeyStruct other)
        {
            return Equals(other.Value1, Value1) && other.Value2 == Value2;
        }
    
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (obj.GetType() != typeof (KeyStruct)) return false;
            return Equals((KeyStruct) obj);
        }
    
        public override int GetHashCode()
        {
            unchecked
            {
                return ((Value1 != null ? Value1.GetHashCode() : 0)*397) ^ Value2.GetHashCode();
            }
        }
    
        public static bool operator ==(KeyStruct left, KeyStruct right)
        {
            return left.Equals(right);
        }
    
        public static bool operator !=(KeyStruct left, KeyStruct right)
        {
            return !left.Equals(right);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I need to create a qt widget, which will act as a parent for
I need to write a class that hold as Dictionary. The Dictionary will be
I need to create a C++ dll that will be called from another program
i need to create a data type (struct in this case) with an array
I need create custom dialog and put JPanel into it. Is it possible?
Need to create a custom DNS name server using C which will check against
I need to create several id's for several structs. Now I want that each
I have a simulation that reads large binary data files that we create (10s
I would like to create a construct similar to std::iterator_traits::value_type that can work seamlessly

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.