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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:20:58+00:00 2026-05-16T11:20:58+00:00

Please compare these two codes. I can’t understand why former one didn’t work while

  • 0

Please compare these two codes. I can’t understand why former one didn’t work while latter one work perfectly.

// With loop - not work
for (int i = 0; i < 5; i++)
{
    Location l = new Location();
    l.Identifier = i.ToString();
    _locations.Add(l);
}
////

Dictionary<Location, Route> _paths = new Dictionary<Location, Route>();
foreach (Location loc in _locations)
{
    _paths.Add(loc, new Route(loc.Identifier));
}

Location start = new Location();
start.Identifier = "1";
_paths[start].Cost = 0;       //raised Key not exists error

Here is working version…

// Without Loop - it work
Location l1 = new Location();
l1.Identifier = "1";
_locations.Add(l1);

Location l2 = new Location();
l2.Identifier = "2";
_locations.Add(l2);

Location l3 = new Location();
l3.Identifier = "3";
_locations.Add(l3);
/////

Dictionary<Location, Route> _paths = new Dictionary<Location, Route>();
foreach (Location loc in _locations)
{
    _paths.Add(loc, new Route(loc.Identifier));
}

Location start = new Location();
start.Identifier = "1";
_paths[start].Cost = 0;

Any ideas? Thanks.

Edit: Location Class

public class Location

{
    string _identifier;
    public Location()
    {

    }

    public string Identifier
    {
        get { return this._identifier; }
        set { this._identifier=value; }
    }

    public override string ToString()
    {
        return _identifier;
    }
}
  • 1 1 Answer
  • 5 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-16T11:20:59+00:00Added an answer on May 16, 2026 at 11:20 am

    Neither should work unless you override Equals and GetHashCode in your Location class so that the Dictionary matches Location key objects based on equality of their Identifier rather than object equality.

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

Sidebar

Related Questions

Can somebody please compare these two logging mechanism?
Please see following examples first, and which one is better? Could you compare some
Can someone please explain the functions of these three instructions? ORG 1000H MOV AX,CS
Would anyone please be able to tell me the most efficient way to compare
could soemone help me with the following condition, please? I'm trying to compare $price
I want to compare two floats in PHP, like in this sample code: $a
This one is probably for someone with some knowledge of music theory. Humans can
I want to compare two lists, based on a filter expression; not sure how
I need to compare two XMLs, which I get in the form of string
I want to compare two lists in java and build two lists oneFound and

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.