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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:58:22+00:00 2026-05-10T17:58:22+00:00

I have a specialized list that holds items of type IThing : public class

  • 0

I have a specialized list that holds items of type IThing:

public class ThingList : IList<IThing> {...}  public interface IThing {     Decimal Weight { get; set; }     Decimal Velocity { get; set; }     Decimal Distance { get; set; }     Decimal Age { get; set; }     Decimal AnotherValue { get; set; }      [...even more properties and methods...] } 

Sometimes I need to know the maximum or minimum of a certain property of all the things in the list. Because of ‘Tell don’t ask’ we let the List figure it out:

public class ThingList : IList<IThing> {     public Decimal GetMaximumWeight()     {         Decimal result = 0;         foreach (IThing thing in this) {             result = Math.Max(result, thing.Weight);         }         return result;     } } 

Thats very nice. But sometimes I need the minimum weight, sometimes the maximum velocity and so on. I don’t want a GetMaximum*()/GetMinimum*() pair for every single property.

One solution would be reflection. Something like (hold your nose, strong code smell!):

Decimal GetMaximum(String propertyName); Decimal GetMinimum(String propertyName); 

Are there any better, less smelly ways to accomplish this?

Thanks, Eric

Edit: @Matt: .Net 2.0

Conclusion: There is no better way for .Net 2.0 (with Visual Studio 2005). Maybe we should move to .Net 3.5 and Visual Studio 2008 sometime soon. Thanks, guys.

Conclusion: There are diffent ways that are far better than reflection. Depending on runtime and C# version. Have a look at Jon Skeets answer for the differences. All answers are are very helpful.

I will go for Sklivvz suggestion (anonymous methods). There are several code snippets from other people (Konrad Rudolph, Matt Hamilton and Coincoin) which implement Sklivvz idea. I can only ‘accept’ one answer, unfortunately.

Thank you very much. You can all feel ‘accepted’, altough only Sklivvz gets the credits 😉

  • 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-10T17:58:22+00:00Added an answer on May 10, 2026 at 5:58 pm

    Yes, you should use a delegate and anonymous methods.

    For an example see here.

    Basically you need to implement something similar to the Find method of Lists.

    Here is a sample implementation

    public class Thing {     public int theInt;     public char theChar;     public DateTime theDateTime;          public Thing(int theInt, char theChar, DateTime theDateTime)     {         this.theInt = theInt;         this.theChar = theChar;         this.theDateTime = theDateTime;     }          public string Dump()     {         return string.Format("I: {0}, S: {1}, D: {2}",              theInt, theChar, theDateTime);     } }  public class ThingCollection: List<Thing> {     public delegate Thing AggregateFunction(Thing Best,                          Thing Candidate);          public Thing Aggregate(Thing Seed, AggregateFunction Func)     {         Thing res = Seed;         foreach (Thing t in this)          {             res = Func(res, t);         }         return res;     } }  class MainClass {     public static void Main(string[] args)     {         Thing a = new Thing(1,'z',DateTime.Now);         Thing b = new Thing(2,'y',DateTime.Now.AddDays(1));         Thing c = new Thing(3,'x',DateTime.Now.AddDays(-1));         Thing d = new Thing(4,'w',DateTime.Now.AddDays(2));         Thing e = new Thing(5,'v',DateTime.Now.AddDays(-2));                  ThingCollection tc = new ThingCollection();                  tc.AddRange(new Thing[]{a,b,c,d,e});                  Thing result;          //Max by date         result = tc.Aggregate(tc[0],              delegate (Thing Best, Thing Candidate)              {                  return (Candidate.theDateTime.CompareTo(                     Best.theDateTime) > 0) ?                      Candidate :                      Best;               }         );         Console.WriteLine("Max by date: {0}", result.Dump());                  //Min by char         result = tc.Aggregate(tc[0],              delegate (Thing Best, Thing Candidate)              {                  return (Candidate.theChar < Best.theChar) ?                      Candidate :                      Best;              }         );         Console.WriteLine("Min by char: {0}", result.Dump());                    } } 

    The results:

    Max by date: I: 4, S: w, D: 10/3/2008 12:44:07 AM
    Min by char: I: 5, S: v, D: 9/29/2008 12:44:07 AM

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

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I'm pretty sure that SQL won't start. However you can… May 11, 2026 at 11:27 am
  • added an answer This is called a Cartesian Product. When you join the… May 11, 2026 at 11:27 am
  • added an answer You can do it using AssociateWith. This will work: var… May 11, 2026 at 11:27 am

Related Questions

I have a specialized list that holds items of type IThing : public class
I have a pretty sizeable object graph that I have serialized to a file
I have an object that is mapped to a cookie as a serialized base-64
I have an object graph serialized to xaml. A rough sample of what it
I have a web-service that I will be deploying to dev, staging and production.
I have a .Net desktop application with a TreeView as one of the UI
I have a Queue<T> object that I have initialised to a capacity of 2,
I have a complete XML document in a string and would like a Document
I have a regex that is going to end up being a bit long
I have a custom validation function in JavaScript in a user control on a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.