如何将Struct转换为类

本文关键字:转换 Struct | 更新日期: 2023-09-27 18:29:23

所以我对C#还很陌生,我的任务是将我的student_data结构转换为一个类,我在网上查找时发现的很多信息都与C++有关,但我觉得这些信息没有用。代码按原样运行,但我正在努力修改它以适应我的任务。它是一个控制台程序,可以打印出数组中的所有值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Student
{
    class Program
    {
        public struct student_data
        {
            public string forename;
            public string surname;
            public int id_number;
            public float averageGrade;
            public string ptitle;
            public string pcode;
        }
        public struct module_data
        {
            public string mcode;
            public string mtitle;
            public int mark;
        }
        static void populateStruct(out student_data student, string fname, string surname, int id_number, string ptitle, string pcode)
        {
            student.forename = fname;
            student.surname = surname;
            student.id_number = id_number;
            student.averageGrade = 0.0F;
            student.ptitle = ptitle;
            student.pcode = pcode;
        }
        static void populateModule(out module_data module, string mcode, string mname, int score)
        {
            module.mcode = mcode;
            module.mtitle = mname;
            module.mark = score;
        }
        static void Main(string[] args)
        {
            student_data[] students = new student_data[4];
            populateStruct (out students[0], "Mark", "Anderson", 1, "Title1", "Code1");
            populateStruct (out students[1], "John", "Smith", 2, "Title2", "Code2");
            populateStruct (out students[2], "Tom", "Jones", 3, "Title3", "Code3");
            populateStruct (out students[3], "Ewan", "Evans", 4, "Title4", "Code4");
            module_data[] modules = new module_data[4];
            populateModule (out modules [0], "MCode1", "MTitle1", 1);
            populateModule (out modules [1], "MCode2", "MTitle2", 2);
            populateModule (out modules [2], "MCode3", "MTitle3", 3);
            populateModule (out modules [3], "MCode4", "MTitle4", 4);
            foreach (student_data value in students) {
                printStudent(value);
            }
            foreach (module_data value in modules) {
                printModule(value);
            }
        }
        static void printStudent(student_data student)
        {
            Console.WriteLine("Name: " + student.forename + " " + student.surname);
            Console.WriteLine("Id: " + student.id_number);
            Console.WriteLine("Av grade: " + student.averageGrade);
            Console.WriteLine("Programme Title: " + student.ptitle);
            Console.WriteLine("Programme Code: " + student.pcode);
            Console.WriteLine(" ");
        }
        static void printModule(module_data module)
        {
            Console.WriteLine("Module Code: " + module.mcode);
            Console.WriteLine("Module Title: " + module.mtitle);
            Console.WriteLine("Module Mark: " + module.mark);
            Console.WriteLine(" ");
        }
    }
}

如何将Struct转换为类

您只需要将其从struct重命名为class。我还建议您查看微软针对C#的样式指南和命名约定

试试这段代码,我认为你必须花一些时间阅读面向对象编程。实例化CLASS有不同的方法。我写了一个简单的方法,这样你就可以理解了。

此外,请阅读好文章并试用样品。课程(C#编程指南)

  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Threading.Tasks;
  namespace ConsoleApplication1
  {
  class Program
   {
    public class student_data
    {
        public string forename;
        public string surname;
        public int id_number;
        public float averageGrade;
        public string ptitle;
        public string pcode;
    }
    public class module_data
    {
        public string mcode;
        public string mtitle;
        public int mark;
    }

    static void printStudent(student_data student)
    {
        Console.WriteLine("Name: " + student.forename + " " + student.surname);
        Console.WriteLine("Id: " + student.id_number);
        Console.WriteLine("Av grade: " + student.averageGrade);
        Console.WriteLine("Programme Title: " + student.ptitle);
        Console.WriteLine("Programme Code: " + student.pcode);
        Console.WriteLine(" ");
    }
    static void printModule(module_data module)
    {
        Console.WriteLine("Module Code: " + module.mcode);
        Console.WriteLine("Module Title: " + module.mtitle);
        Console.WriteLine("Module Mark: " + module.mark);
        Console.WriteLine(" ");
    }
    static void Main(string[] args)
    {
        List<student_data> students = new List<student_data>();
        student_data student = new student_data();
        student.forename = "Mark";
        student.surname = "Anderson";
        student.id_number = 1;
        student.ptitle = "Title1";
        student.pcode = "Code1";
        students.Add(student);
        List<module_data> modules = new List<module_data>();
        module_data module = new module_data();
        module.mcode = "MCode1";
        module.mtitle = "MTitle1";
        module.mark = 10;
        modules.Add(module);
        foreach (student_data value in students)
        {
            printStudent(value);
        }
        foreach (module_data value in modules)
        {
            printModule(value);
        }
        Console.ReadLine();
    }
  }
}