繁体   English   中英

C#Struct / ArrayList和最小值/最大值/平均值

[英]C# Struct/ArrayList and min/max/average values

我正在学习C#的过程中,对C#结构有些困惑,并从中查找最小值,最大值和平均值。

我的结构声明如下:

    [Serializable]
    private struct students
    {   // Constructor Fills with default values

        public students(int x)
        {
            studentImage = " ";
            enrollmentDate = " ";
            firstName = " ";
            surname = " ";;
            englishGrade = 0;
            scienceGrade = 0;
            mathGrade = 0;
        }

        // Data types to be used in the Structure
        public string studentImage, enrollmentDate, firstName, surname;
        public int englishGrade, scienceGrade, mathGrade;

    }

   private ArrayList studentList; //This is used to work with the data

在结构中,列出了多个学生及其科目成绩。 我希望浏览“记录”并获得以下内容;

  • 结构/数组列表中的最低englishGrade
  • 来自Structure / ArrayList的最大englishGrade
  • 来自Structure / ArrayList的平均EnglishGrade

我已经考虑过对ArrayList进行排序,但是我不能使用这种方式,因为我需要基于firstName来排序数据。

有人能指出我正确的方向吗? /帮忙吗?

假设您确实要使用ArrayList

studentList = new ArrayList();
//Fill data
var maxEnglish = studentList.
    OfType<students>().
    Max(student => student.englishGrade);
var minEnglish = studentList.
    OfType<students>().
    Min(student => student.englishGrade);
var avgEnglish = studentList.
    OfType<students>().
    Average(student => student.englishGrade);

添加到您的using指令后:

using System.Linq;

但是,为什么不使用具有通用类型的List<students> ,例如List<students>呢? 这将使您的代码更具可读性和效率。 此外,考虑到您将students声明为structArrayList每个插入都代表一个拳击。 这不是表现。

我将对您的代码进行以下编辑:

  • students更改为班级,然后将其重命名为Student 通常,如果一个struct大小的字段加起来超过16个字节,则最好使用一个类。
  • studentList更改为List<Student>
  • 删除对OfType的调用,这将是多余的。

the only purpose of the struct is to hold or pass around a semantically-fixed set of discrete data items, no methods write to this or its fields outside the constructor and property setters, the entire state of the struct is encapsulated in public fields which can legitimately take on any combination of values which would be valid for their respective types, and whose default values within the struct should match the default values in their respective types. 使用透明结构类型(一个与所有公共字段)是好的, 该结构的唯一目的是保持或绕过一个语义固定组离散的数据项的,没有方法写入this或它的构造以外的字段和属性设置器,将结构的整个状态封装在公共字段中,这些字段可以合法地采用对各自类型有效的值的任意组合,并且结构内的默认值应与各自类型中的默认值匹配。 您的students类型几乎符合该标准,但并不完全符合该标准,因为它似乎代表了学生特征的任意选择,并且没有特别的理由相信每个需要有关学生信息的人都需要相同的信息。 此外,该设计似乎无需对每个学生恰好具有三个年级的事实进行硬编码。 在某些情况下,这种假设是合理的。 例如,由于设计用于二维点的代码对于三维点可能是无用的,因此最好使用透明的结构来保持二维点的坐标。 如果出于某种目的需要3d点,则设计用于3d点的代码将无法使用它,因此可以简单地将3d点设置为一种新类型。 相比之下,可以合理地认为将来可能会有第四种等级,并且如果代码能够与第四等级“兼容”,那将是可取的,但是当使用结构时,实际上并没有任何好处。实现这一目标的方法。

不要使用ArrayList 看一看通用列表

private List<students> studentList;

用法:

var studentWithMaxGrade = studentList.Max(student => student.englishGrade);
var studentWithMinGrade = studentList.Min(student => student.englishGrade);
var averageGrade = studentList.Average(student => student.englishGrade);

您可能还想使自己熟悉C#命名约定

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM