在C#结构的数组 [英] Array of structs in C#

查看:118
本文介绍了在C#结构的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用结构的数组从用户那里得到输入,然后打印:

I'm trying to get input from user using array of structs and then print it:

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

namespace CA4
{
class Program
{
    static void Main(string[] args)
    {
        StudentDetails[,] student = new StudentDetails[5, 1];

        Console.WriteLine("Please enter the unit code:");
        student[0, 0].unitCode = Console.ReadLine();

        Console.WriteLine("Please enter the unit number:");
        student[1, 0].unitNumber = Console.ReadLine();

        Console.WriteLine("Please enter first name:");
        student[2, 0].firstName = Console.ReadLine();

        Console.WriteLine("Please enter last name:");
        student[3, 0].lastName = Console.ReadLine();

        Console.WriteLine("Please enter student mark:");
        student[4, 0].studentMark = int.Parse(Console.ReadLine());

        for (int row = 0; row < 5; row++)
        {
            Console.WriteLine();
            for (int column = 0; column < 1; column++)
                Console.WriteLine("{0} ", student[row, column]);
        }

        Console.ReadLine();
    }

    public struct StudentDetails
    {
        public string unitCode; //eg CSC10208
        public string unitNumber; //unique identifier
        public string firstName; //first name
        public string lastName;// last or family name
        public int studentMark; //student mark
    }

}
}

输入所有数据后,不幸的是我得到:

Unfortunately after entering all the data I get:

CA4.Program+StudentDetails

CA4.Program+StudentDetails

CA4.Program+StudentDetails

CA4.Program+StudentDetails

CA4.Program+StudentDetails

它不会崩溃,而不是只是我输入得到上面的5行数据。

It doesn't crash, just instead of the data that I entered get the above 5 lines.

我知道为什么它不工作的原因是,我因为没有他们就没有问题,不正确地使用结构。

I know that the reason why it doesn't work is that I don't use the structs correctly because without them there is no problem.

有人可以请帮助我,告诉我如何使用结构propely在上面的例子。谢谢

Can somebody please help me and tell me how to use structs propely in the example above. Thanks

干杯,

n1te

推荐答案

您调用 Console.WriteLine({0},学生[行,列]); 被隐式调用StudentDetails结构,刚刚写出默认情况下,结构类型的名称的ToString()方法。重写toString()方法:

Your call to Console.WriteLine("{0} ", student[row, column]); is implicitly calling the ToString() method of the StudentDetails struct, which just writes out the name of the struct type by default. Override the ToString() method:

public struct StudentDetails
{
    public string unitCode; //eg CSC10208
    public string unitNumber; //unique identifier
    public string firstName; //first name
    public string lastName;// last or family name
    public int studentMark; //student mark

    public override string ToString()
    {
        return string.Format("{0},{1},{2},{3},{4}", unitCode,
               unitNumber,firstName,lastName,studentMark);
    }
}

然而,更大的问题是,你通过声明数组 StudentDetails [,] =学生新StudentDetails [5,1]设置5个不同的StudentDetails结构...属性; 并要求用户约在不同的点阵列,即学生[0,0] 然后学生[在结构输入的详细说明1,0] ,你是不是使一个StudentDetails对象,并对其设置属性,创建5的不同的的StudentDetails对象。

However, the larger issue is that you are setting the properties of 5 different StudentDetails structs... by declaring an array StudentDetails[,] student = new StudentDetails[5, 1]; and asking the user to input details about the structs at different points in the array, i.e. student[0, 0] then student[1,0], you aren't making one StudentDetails object and setting properties on it, you created 5 different StudentDetails objects.

为什么要使用一个数组?如果你想给用户填单StudentDetails对象,只是做

Why are you using an array? If you want to the user to fill in a single StudentDetails object, just do

StudentDetails student = new StudentDetails();

Console.WriteLine("Please enter the unit code:");
student.unitCode = Console.ReadLine();

Console.WriteLine("Please enter the unit number:");
student.unitNumber = Console.ReadLine();

...

然后将它写出来:

Then to write it out:

Console.WriteLine("{0}", student);

这将使用您在StudentDetails结构中声明的ToString()方法。 (toString()方法被称为每当一个对象需要被转换为字符串,你只是不总是写吧)

This will use the ToString() method you declared in your StudentDetails struct. (ToString() is called whenever an object needs to be converted to a string, you just don't always have to write it)

希望这有助于。

这篇关于在C#结构的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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