本人新手,很多都是摘抄,借鉴,用于留笔记用,以备不时之需,若有看客,轻喷。

 

首先先建立一个数组array;
int[] array = new int[] { 1,2,4,3,5,6};

方法一:直接使用(按顺序分别为,最小值,最大值,平均值)


array.min();
array.max();
array.average();

这种方法在数组,list等都可使用,简单方便。

方法二:方法一不能满足需求时可以看看方法二能不能用。

/// <summary>
        /// 最小值
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static int min(int[] array)
        {
            if (array == null) throw new exception("数组空异常");
            int value = 0;
            bool hasvalue = false;
            foreach (int x in array)
            {
                if (hasvalue)
                {
                    if (x < value) value = x;
                }
                else
                {
                    value = x;
                    hasvalue = true;
                }
            }
            if (hasvalue) return value;
            throw new exception("没找到");
        }

 

方法三:
使用ordby升序(降序)排序。排序后的数组第一个元素(最后一个元素)的位置即为所求

list<line> minpoint = new list<line>();
for (int i = 0; i < inpointlist.count; i++)
{
line linetest = new line(node.nodeblock.closepoint, inpointlist[i]);
minpoint.add(linetest);
}
minpoint = minpoint.orderby(c => c.length).tolist();

若有错误,欢迎指正,避免误导,谢谢。