在C#中使用结构来读取数据 [英] Using structs in C# to read data

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

问题描述

说我有定义为这样的

struct Student
{
  int age;
  int height;
  char[] name[12];
}

当我读一个二进制文件,它看起来像

When I'm reading a binary file, it looks something like

List<Student> students = new List<Student>();
Student someStudent;

int num_students = myFile.readUInt32();
for (int i = 0; i < num_students; i++)
{
   // read a student struct
}

我怎么能写我的结构,这样我只需要说沿

How can I write my struct so that I just need to say something along the lines of

someStudent = new Student();



因此​​,它将读取的结构体定义的顺序的文件,允许我得到根据需要用语法值如

So that it will read the file in the order that the struct is defined, and allow me to get the values as needed with syntax like

someStudent.age;



我可以定义学生为一类,并有构造函数读取数据并填充他们,但它不会告发'吨有超过getter / setter方法​​的任何方法,所以我想一个结构会更合适。

I could define the Student as a class and have the constructor read data and populate them, but it wouldn't have any methods beyond getters/setters so I thought a struct would be more appropriate.

或者它不会不管我使用一个类或结构?我见过别人使用结构中的数据块来读取并推断这是一个好的方式做到这一点编写C代码。

Or does it not matter whether I use a class or struct? I've seen others write C code using structs to read in blocks of data and figured it was a "good" way to do it.

推荐答案

如果您想序列化/反序列化结构

如果你想读/整个结构写入一个二进制文件(序列化),我建议你看看

If you want to read/write the entire struct to a binary file (serialization), I suggest you look at

http://stackoverflow.com / A /十四万一千一百七十二分之六十二万九千一百二十○

或者,如果它是一个选择,关注@马克的建议,使用跨平台的串行器。我个人建议 protobuf网这恰好写了由@Marc。

Or, if it is an option for you, follow @Marc's advice and use a cross-platform serializer. Personally I would suggest protobuf-net which just happens to have been written by @Marc.

如果您是从任意文件格式加载

就像一个的,一个的结构的可以有一个接受多个参数的构造函数。事实上,这是一般明智的的制定者提供了的结构的。这样做允许的值的结构的要改变它建成后,这通常会导致编程错误,因为许多开发人员没有意识到的结构的事实是值类型值语义的。

Just like a class, a struct can have a constructor that accepts multiple parameters. In fact, it is generally wise to not provide setters for a struct. Doing so allows the values of the struct to be changed after it is constructed, which generally leads to programming bugs because many developers fail to appreciate the fact that struct is a value type with value semantics.

我会建议提供一个单一的构造函数初始化的结构的,从文件到临时变量读取值,然后构建的结构的一个构造函数。

I would suggest providing a single constructor to initialize your struct, reading the values from the file into temporary variables, and then constructing the struct with a constructor.

public stuct MyStruct
{
    public int Age { get; private set; }
    public int Height { get; private set; }
    private char[] name;
    public char[] Name 
    {
        get { return name; }
        set
        {
            if (value.Length > 12) throw new Exception("Max length is 12");
            name = value;
        }
    }
    public MyStruct(int age, int height, char[] name)
    {
    }
}

要进一步深入(可以初始化后,被改变的)可变结构的危险,我建议

To dig further into the perils of mutable structs (ones that can be changed after initialized) I suggest

为什么是可变的结构邪?

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

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