SOF User
SOF User

Reputation: 7830

Get The Max Value of double from List<object> in .Net 2.0

  List<object> lst = new List<object>();

How can I get max value from this list it contains double value.

Upvotes: 0

Views: 1445

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500285

Personally I would use LINQBridge to get you LINQ goodness:

double max = lst.OfType<double>().Max();

It might be overkill to use LINQBridge just for a single "find the maximum element" query, but of course you can then use it everywhere else in your project too... and your code will be more idiomatic when you eventually move off .NET 2 :)

Upvotes: 2

Kikaimaru
Kikaimaru

Reputation: 1851

double max = double.MinValue;
foreach (object item in lst)
{
   if (item is double)
   {
       if ((double)item > max)
       {
           max = (double)item;
       }
   }
}

Upvotes: 2

Related Questions