C# Minimum, Maximum and Average of a custom class?

Hi,

I am trying to calculate a Minimum, Maximum and Average of a List of classes using a Timespan property of this class.

Can I please have some code to do this?

From the example below I would like to be able to return a single "File_Stat" for each of the above mentioned.

Thanks,

Ward


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TimeSpanTest
{
    class File_Stat
    {
        public TimeSpan time_span;
        public string Code;
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<File_Stat> timespan_list = new List<File_Stat>();
            File_Stat my_file_stat;

            my_file_stat = new File_Stat();
            my_file_stat.time_span = new TimeSpan(0, 0, 0, 0, 1);
            my_file_stat.Code = "A";
            timespan_list.Add(my_file_stat);

            my_file_stat = new File_Stat();
            my_file_stat.time_span = new TimeSpan(0, 0, 0, 0, 2);
            my_file_stat.Code = "B";
            timespan_list.Add(my_file_stat);

            my_file_stat = new File_Stat();
            my_file_stat.time_span = new TimeSpan(0, 0, 0, 0, 3);
            my_file_stat.Code = "C";
            timespan_list.Add(my_file_stat);

        }
    }
}

Open in new window

LVL 1
whorsfallAsked:
Who is Participating?
 
Nitin SontakkeDeveloperCommented:
As I keep stating, there are no magical solutions to such problems. You will obviously have to write code to implement the desired function. There is no way for the language or a framework to know (or assume) what and how you wish to implement a particular function.

Fortunately, languages such as C# provide sufficient built-in ground functions to achieve our desired solutions. Here is the modified code with methods implemented.

using System;
using System.Collections.Generic;

namespace TimeSpanTest
{
  class File_Stat : IComparable
  {
    public TimeSpan time_span { get; set; };
    public string Code { get; set; };

    public File_Stat() { }

    public File_Stat(String code, TimeSpan timeSpan) : this()
    {
      Code = code;
      time_span = timeSpan;
    }

    public static File_Stat Minimum(List<File_Stat> FileStatList)
    {
      FileStatList.Sort();
      return FileStatList[0];
    }

    public static File_Stat Maximum(List<File_Stat> FileStatList)
    {
      FileStatList.Sort();
      return FileStatList[FileStatList.Count -1];
    }

    public static File_Stat Average(List<File_Stat> FileStatList)
    {
      Int64 ms = 0;
      foreach (File_Stat fs in FileStatList)
      {
        ms += fs.time_span.Ticks;
      }
      Int64 ams = ms / FileStatList.Count;
      return new File_Stat("Average", new TimeSpan(ams));
    }

    public override string ToString()
    {
      return Code + " -> " + time_span.ToString();
    }

    public int CompareTo(object obj)
    {
      if (!(obj is File_Stat))
        return 0;

      File_Stat fs = (File_Stat)obj;
      return time_span.CompareTo(fs.time_span);
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      List<File_Stat> timespan_list = new List<File_Stat>()
      {
        new File_Stat("A", new TimeSpan(0, 0, 0, 0, 1)),
        new File_Stat("B", new TimeSpan(0, 0, 0, 0, 2)),
        new File_Stat("C", new TimeSpan(0, 0, 0, 0, 3)),
      };

      Console.WriteLine(File_Stat.Minimum(timespan_list).ToString());
      Console.WriteLine(File_Stat.Maximum(timespan_list).ToString());
      Console.WriteLine(File_Stat.Average(timespan_list).ToString());
      Console.ReadKey();
    }
  }
}

Open in new window


And yes, special 'Thank you!' for providing boilerplate code. Not many developers think of providing this basic information in readily usable format while asking questions.
0
 
Dustin SaundersDirector of OperationsCommented:
As Nitin mentions, you will have to code it.  Alternatively, you can create a class that is a collection of FileStat classes and have the methods part of that class:

FileStat Class:
    public class FileStat
    {
        public TimeSpan TimeSpan { get; set; }
        public string Code { get; set; }
    }

Open in new window


FileStats Class:
   public class FileStats
    {
        public List<FileStat> Files { get; set; } = new List<FileStat>();
        
        public FileStat Minimum
        {
            get
            {
                return (Files.OrderBy(item => item.TimeSpan)).First();
            }
        }

        public FileStat Maximum
        {
            get
            {
                return (Files.OrderBy(item => item.TimeSpan)).Last();
            }
        }

        public FileStat Average
        {
            get
            {
                Int64 ms = 0;
                foreach (FileStat fileStat in Files)
                {
                    ms += fileStat.TimeSpan.Ticks;
                }
                return new FileStat
                {
                    Code = "Average",
                    TimeSpan = new TimeSpan(ms / Files.Count)
                };
            }
        }
    }

Open in new window


Then you can access the methods off your class:
        private void DoThing()
        {
            FileStats fileStats = new FileStats
            {
                Files = new List<FileStat>
                {
                    new FileStat{ Code= "A", TimeSpan= new TimeSpan(0, 0, 0, 0, 1) },
                    new FileStat{ Code= "B", TimeSpan= new TimeSpan(0, 0, 0, 0, 2) },
                    new FileStat{ Code= "C", TimeSpan= new TimeSpan(0, 0, 0, 0, 3) }
                }
            };

            Int64 min = fileStats.Minimum.TimeSpan.Ticks;
            Int64 max = fileStats.Maximum.TimeSpan.Ticks;
            Int64 avg = fileStats.Average.TimeSpan.Ticks;
        }

Open in new window

0
 
whorsfallAuthor Commented:
Hi Nitin and Dustin,

Thanks for the comments great code for me to look at.

As a bit of further edification for me.

Can you point me to some Microsoft doco or give me a term so I can do some background reading. Something that matches what has been done in the code.

e.g. Generic overloading. But I am not sure of course what the precise term I should have here.

Thanks,

Ward
0
 
Dustin SaundersDirector of OperationsCommented:
What Nitin has is implementing an Interface:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/

It's very similar to an abstract class:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods

There are a few differences, mainly a class can inherit multiple interfaces but only one abstract.

In my code, I've just created 2 classes.  One for the object of FileStat, and one that contains those custom objects and the custom methods I want to perform based off that 2nd class.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.