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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:28:06+00:00 2026-05-28T16:28:06+00:00

I have a custom class containing 2 public variables: 1 is a string and

  • 0

I have a custom class containing 2 public variables: 1 is a string and 1 is an integer. I then make a list of this class, in the list I need the string of the class to be unique, if the string already exists in the list I don’t want to add it again but I do want to combine the corresponding integers. here is an example of the custom class and list.

public class myItems
{
    public string itemName;
    public int count;
}

List<myItems> items = new List<myItems>();

myItems e = new myItems();
e.symbol = "pencil";
e.count = 3;
items.Add(e);

myItems e1 = new myItems();
e1.symbol = "eraser";
e1.count = 4;
items.Add(e1);

myItems e2 = new myItems();
e1.symbol = "pencil";
e1.count = 3;
items.Add(e5);

So for the final list i want to it contain: pencil 7, eraser 4. I have been using the contains function on the list to check if it already exists but it only returns true if both the string and integer are the same.

Is there a way to only match on the string?

  • 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-28T16:28:06+00:00Added an answer on May 28, 2026 at 4:28 pm

    Another way to do it would be to use LINQ:

    public class myItems
    {
        public string itemName;
        public int count;
    }
    
    List<myItems> items = new List<myItems>();
    
    myItems e = new myItems();
    e.symbol = "pencil";
    e.count = 3;
    Add(items, e);
    
    myItems e1 = new myItems();
    e1.symbol = "eraser";
    e1.count = 4;
    Add(items, e1);
    
    myItems e2 = new myItems();
    e1.symbol = "pencil";
    e1.count = 3;
    Add(items, e5);
    
    public void Add(List<myItems> list, myItems newItem)
    {
        var item = list.SingleOrDefault(x => x.symbol == newItem.symbol);
    
        if(item != null)
        {
            item.count += newItem.count;
        }
        else
        {
            list.Add(newItem);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have a list containing a custom class, MyClass MyClass has properties, which
So I have a custom type Foo : public class Foo { public string
I have a custom class that implements that IComparable. This class is stored in
I have a custom class that implements ICollection , and this class is readonly,
I have a custom class Contact . I am trying to bind a List<Contact>
I have a custom class that uses boost mutexes and locks like this (only
Say you have a custom class call it Foo. When you then have an
I have a custom List which carries some attributes I need to serialize along
I have 2 Lists containing entries of a custom class I have created. Basically,
I have a custom class Spieltag containing a property SpieltagDaten (custom class). SPieltagDaten has

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.