如何在C#中调用类值类? [英] How to call class value class in C#?

查看:97
本文介绍了如何在C#中调用类值类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。

我的班级叫做学生。并且我将failstudents变量保存在类中。

我想在另一个类中调用此变量并且使用if if语句。

我是否称之为方式。



如何拨打另一个班级和if语句。









公开列表< string> failstudents =新列表< string>();



我尝试过:



Students.cs



公开列表< string> failstudents =新列表< string>();



Process.cs


$ b $bİf(Students.failstudents .count!= 0)

{



}

Hello.
I have my class called Students. And I keep the failstudents variable in class like this.
I would like to call this variable in another class and ı use want to if statement.
Do I call it that way.

How to call another class and if statement.




public list <string> failstudents=new list<string>();

What I have tried:

Students.cs

public list <string> failstudents=new list<string>();

Process.cs

İf(Students.failstudents.count !=0)
{

}

推荐答案

那里类可以有两种类型的元素: static 元素,实例元素(元素所在的元素)字段,属性,方法,事件或委托)



所有实例共享 static 元素,并通过类名访问。

实例元素对于类的每个不同实例是唯一的,并通过持有实例的变量访问参考。



想想汽车片刻:所有汽车都有颜色 - 但颜色取决于您所说的具体汽车。我的车是黑色的;你的车是红色的;这辆车是绿色的;那车是蓝色的。 Color 是Car类的实例属性,因为您需要具有Car的特定实例才能询问它的颜色是什么颜色? - 你不能说车是什么颜色的?因为没有说你的意思是什么就没意义了。

但是汽车还有静态属性:你可以问车有多少车轮? 因为所有车都有四个轮子。 (如果它有两个,它将是一辆摩托车,而不是一辆汽车)



因此,如果你想通过类名来访问一个List,就像你的例子一样需要对所有实例都是通用的,即它需要 static

There are two types of "elements" that a class can have: static elements, and instance elements (where an element is a field, property, method, event, or delegate)

A static element is shared by all instances, and is accessed via the class name.
An instance element is unique to each different instance of the class and is accessed via the variable holding the instance reference.

Think about cars for a moment: all cars have a colour - but which colour it is depends on which specific car you are talking about. My car is black; your car is red; this car is green; that car is blue. Colour is an instance property of the Car class because you need to have a specific instance of a Car in order to ask the question "what colour is it?" - you can't say "what colour is a car?" because it's meaningless without saying which car you mean.
But cars have static properties as well: you can ask "how many wheels has a car?" because all cars have four wheels. (If it had two, it would be a motorbike, not a car)

So if you want to access a List via the class name as in your example, it needs to be common to all instances, i.e. it needs to be static :
public static List<string> failstudents = new List<string>();


imho,一种使你的意图更清晰的可能方法是区分表示实例的实体/类和表示实例的集合的实体/类,和/或实例的管理。 />


考虑:
imho, one possible way to make your intent more clear is to distinguish between an entity/Class that represents an instance and an entity/Class that represents a collection of instances, and/or management of instances.

Consider:
public class Student
{
    // ctor
    public Student(string name)
    {
        Name = name;
        StudentManager.RegisterStudent(this);
    }

    // fields that hold student data

    public bool IsPassing { get; private set; }

    // ... more student data
    public string Name { get; private set; }

    private double _averagegrade;
    public double AverageGrade {
        get
        {
            return _averagegrade;
        }

        internal set // for future: find a way to only allow Manager class to set this
        {
            _averagegrade = value;

            IsPassing = _averagegrade > StudentManager.FailLevel;

            if (! IsPassing)
            {
                // show a warning ?
                throw new ArgumentOutOfRangeException("", "\r\r\r\nAverage Grade: FAILING\r\n");
            }
        }
    }

    public void AddGrade(double grade)
    {
        StudentManager.AddGrade(this, grade);
    }
}

public static class StudentManager
{
    public static double FailLevel = 50.0;

    // map students to grades
    public static Dictionary<Student, List<double>> StudentGrades = new Dictionary<Student, List<double>>();

    // ctor
    public static void RegisterStudent(Student student)
    {
       // left for you to implement
    }

    public static void AddGrade(Student student, double grade)
    {
       // left for you to implement
    }

    public static double GetAverageGrade(Student student)
    {
        var grades = StudentGrades[student];
        return grades.Sum() / grades.Count;
    }

    public static int NumberOfStudents
    {
        get { return StudentGrades.Count; }
    }

    public static List<Student> GetFailingStudents
    {
       // left for you to implement
    }
}


这篇关于如何在C#中调用类值类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆