Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# contains behaviour

Tags:

c#

Can someone please explain why these two methods are returning different values?

List<CustomerSummary> summaries = new List<CustomerSummary>();

for (var i = 0; i < 10; i++)
{

var summary = new CustomerSummary() { ID = 1, Name = "foo", balance = 50.00 };

if (!summaries.Contains(summary))
    summaries.Add(summary);

}

-

List<CustomerSummary> summaries = new List<CustomerSummary>();

for (var i = 0; i < 10; i++)
{

 var summary = new CustomerSummary() { ID = 1, Name = "foo", balance = 50.00 };

 if (!summaries.Any(s=> s.ID == summary.ID))
     summaries.Add(summary);

}

The first method has 10 items in the list while the second has 1. Why does the first ( Contains() ) method never evaluate as true?

What I'm trying to ask is why are 2 objects of the same type with the same values for each property not evaluating as equivalent (in the first method)?

like image 560
user2865446 Avatar asked Aug 13 '14 04:08

user2865446


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


1 Answers

Your first block of code is creating a new instance inside the loop, and then immediately checking to see if that exact instance is already in the collection. It can't be - you just created it.

You might as well write it like this:

List<CustomerSummary> summaries = new List<CustomerSummary>();

for (var i = 0; i < 10; i++)
{
    var summary = new CustomerSummary { ID = 1, Name = "foo", balance = 50.00 };

    summaries.Add(summary);
}

Your second block of code is checking for the existence of any item in the collection with a matching ID. Since you've hard-coded the ID to be 1, you're only going to add an item the first time around. Every instance created after that will return false in the if statement, and will not be added.

You could change that behavior by changing the ID:

for (var i = 0; i < 10; i++)
{
    var summary = new CustomerSummary { ID = i, Name = "foo", balance = 50.00 };

    if (!summaries.Any(s=> s.ID == summary.ID))
        summaries.Add(summary);
}

Again, the if statement is no longer even necessary, since you know the ID can't possibly already exist in the collection, so you could just remove the check.

like image 119
Grant Winney Avatar answered Sep 18 '22 14:09

Grant Winney