Finds the mean value given a function defining the value from an object to use. - CSharp System

CSharp examples for System:Object

Description

Finds the mean value given a function defining the value from an object to use.

Demo Code

// Licensed under the Apache License, Version 2.0 (the "License");
using System.Linq;
using System.Collections.Generic;
using System;//from w  w  w  .j a  v  a 2s .c o  m

public class Main{
        /// <summary>
        /// Finds the mean value given a function defining the value from an object to use. 
        /// </summary>
        public static float Mean<T>(this ICollection<T> items, Func<T, int> value) {
            return items.Select(value).DefaultIfEmpty().Average(r => (float)r);
        }
        /// <summary>
        /// Finds the mean value given a function defining the value from an object to use. 
        /// </summary>
        public static float Mean<T>(this ICollection<T> items, Func<T, float> value) {
            return items.Select(value).DefaultIfEmpty().Average(r => r);
        }
}

Related Tutorials