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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:17:57+00:00 2026-05-11T09:17:57+00:00

I am curious on how the following code works, especially the part the contains

  • 0

I am curious on how the following code works, especially the part the contains the delegate stuff since I am new to it. Also, are there any bottlenecks in the code, for example, would using a SortedDictionary be better than using a List and then sorting it using LINQ? Ok, here is the code:

public class ColorManager {     private List<ColorData> colorCodes = new List<ColorData>();      public List<ColorData> ColorCodes     {         get         {             var sortList = from a in this.colorCodes                            orderby a.colorCode ascending                            select a;              return sortList.ToList();         }     }      public void FillData(DataTable table)     {         for(int row = 0; row < table.Rows.Count; row++)         {             ColorData cData = new ColorData();             string name = table.Rows[row]['Color'].ToString().Trim();              if(!this.colorCodes.Exists(                 delegate(ColorData e)                 {                     return e.ColorCode == name;                 }))             {                 cData.Add(table.Rows[row]['Color'].ToString());                 this.colorCodes.Add(cData);             }             else             {                 this.colorCodes.Find(                     delegate(ColorData e)                     {                         return e.ColorCode == name;                     }).Count++;             }         }      } } 
  • 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. 2026-05-11T09:17:57+00:00Added an answer on May 11, 2026 at 9:17 am

    I am curious on how the following code works, especially the part the contains the delegate stuff since I am new to it.

    First take a look at this ColorCodes property accessor:

            var sortList = from a in this.colorCodes                        orderby a.colorCode ascending                        select a; 

    It returns a list of all the color codes (this.colorCodes) in ascending order (...orderby a.colorCode ascending). Simple so far. How about the meatier FillData method?

    First, we’ll loop over every row in this table:

        for(int row = 0; row < table.Rows.Count; row++)     { 

    Then, we’re going to look at the Color column of the current row.

            ColorData cData = new ColorData();         string name = table.Rows[row]['Color'].ToString().Trim(); 

    If that column has no match in our list of color codes, this if condition is true. The Exists method takes a single-argument function that returns a bool, which is what this anonymous delegate is: a single-argument (ColorData e) function that returns a boolean (e.ColorCode == name). It gets negated (!this.[...]), so if a match is found, the condition is false.

            if(!this.colorCodes.Exists(             delegate(ColorData e)             {                 return e.ColorCode == name;             }))         {             cData.Add(table.Rows[row]['Color'].ToString());             this.colorCodes.Add(cData);         } 

    Otherwise, if there’s a match, find the name using another anonymous delegate, and add one to the count of items of that color.

            else         {             this.colorCodes.Find(                 delegate(ColorData e)                 {                     return e.ColorCode == name;                 }).Count++;         }     }  

    Note that this code is somewhat inefficient, since the two different anonymous delegates really do the same thing and could be shared.

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

Sidebar

Related Questions

The following code works, but I'm curious as to why I need the Path
I'm curious if following code would be considered safe? using (SqlConnection cn = new
I'm curious as to why the following code works (run under the VS debugger):
i have encountered the following curious piece of code: function foo(){ works = {hello:world};
I'm curious to know why the following code behaves differently? The following does not
New to LINQ.. I am curious as to the syntax to do the following
I have the following code: Hashtable env1 = new Hashtable(); env1.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,com.ibm.websphere.naming.WsnInitialContextFactory); log.info(Executed step 1);
I have a curious problem with some javascript. In the following code the browser
The following code works great in IE, but not in FF or Safari. I
I have the following code which works as expected: #include <iostream> using namespace std;

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.