简体   繁体   English

C#-列表清单

[英]C# - List of Lists

I am coding in C# and I have a class with a property of type List<List<T>> that gets initialized in the constructor. 我使用C#进行编码,并且具有一个类,其属性类型为List <List <T >>,该类在构造函数中初始化。 The code is as follows: 代码如下:

public class MyMatrix<T>
{
    private readonly List<List<T>> matrix;

    public MyMatrix(int dimension)
    {
        this.matrix = new List<List<T>>();
        for (int i = 0; i < dimension; i++)
        {
            List<T> subList = new List<T>();
            this.matrix.Add(subList);
        }
    }
    .....

The problem is that if I create a new object of type MyMatrix the sublists are empty so if I invoke the ToString() method of the class or any other method that returns the values contained in the sublists I get an OutOfOrder Exception as expected. 问题是,如果我创建一个MyMatrix类型的新对象,则子列表为空,因此,如果我调用该类的ToString()方法或其他任何返回子列表中包含的值的方法,则会得到OutOfOrder异常。 Get and Set methods are as follows: Get和Set方法如下:

public T Get(int row, int column)
    {
        return this.matrix[row][column];
    }

public void Set(int row, int column, T value)
    {
        this.matrix[row].Insert(column, value);
    }

If I initialize the sublists with a Set method then everything is fine obviously. 如果我使用Set方法初始化子列表,那么一切都很好。 I can't change the constructor as it is up to the user of the class to initialize the sublists so it is not possible to know in advance what they are going to contain. 我不能更改构造函数,因为要由类的用户来初始化子列表,因此无法事先知道它们将包含什么。 How would you manage the exceptions in the class methods or would you bother at all? 您将如何在类方法中管理异常,或者根本不打扰?

What about C# 6.0? C#6.0呢?

public T Get(int row, int column)
{
    return this.matrix[row]?[column] ?? default(T);
}

There are several approaches on managing exceptions in your case, and it depends on how you want to use the matrix class: 在您的情况下,有几种方法可以管理异常,这取决于您要如何使用矩阵类:

  1. If you expect users to set values without initializing the row/column, then on the Set method I would just resize the list if necesary to accomodate the row/column arguments. 如果您希望用户在不初始化行/列的情况下设置值,那么在Set方法上,如果需要适应行/列参数,我将调整列表的大小。 You can always insert empty items in the list by using default(T) (this works both with value and reference objects). 您始终可以使用default(T)在列表中插入空项目(这适用于值对象和引用对象)。 In this scenario, the Get method should check if the matrix coordinates exist and otherwise return default(T) so that no exceptions occurr. 在这种情况下,Get方法应检查矩阵坐标是否存在,否则返回default(T)以便不发生异常。

  2. If you expect users to always initialize the matrix, then just leave it as it is and throw exceptions. 如果您希望用户始终初始化矩阵,则只需保持原样并引发异常即可。 This is a clear hint that the application is misbehaving and the programmer must take care of this. 这明确表明应用程序行为异常,程序员必须注意这一点。

  3. If you are trying to implement something like a Sparse Matrix , then using List<T> is not the best way and you should try another approach - for example using Dictionary<int, Dictionary<int, T>> or some sort of linked list. 如果尝试实现稀疏矩阵之类的方法 ,则使用List<T>并不是最佳方法,应尝试另一种方法-例如,使用Dictionary<int, Dictionary<int, T>>或某种形式的链表。 Anyway in this scenario, if you go for the Dictionary approach, you still need to take the same decisions as above (ie. throw if accessing a non existent coordinate or just return default(T) ) 无论如何,在这种情况下,如果您选择使用Dictionary方法,则仍需要做出与上述相同的决定(即,如果访问不存在的坐标或仅返回default(T)抛出)

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

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