如何优化我的代码,什么是c#indexers的vb.net等效?

发布于 2025-02-12 16:34:31 字数 1360 浏览 2 评论 0原文

struct Matrix {
  readonly int Rows, Columns;
  int[,] _Matrix;

  public Matrix(int Rows, int Columns) {
    this.Rows = Rows;
    this.Columns = Columns;
    _Matrix = new int[Rows, Columns]
  }

  public Matrix(int RowsOrColumns) {
    this.Rows = RowsOrColumns;
    this.Columns = RowsOrColumns;
    _Matrix = new int[RowsOrColumns, RowsOrColumns]
  }

  public int this[int Row, int Column] {
    get => _Matrix[Row, Column];
    set => _Matrix[Row, Column] = value;
  }
}

我在C#中写了矩阵结构。我试图用VB编写相同的矩阵结构,但是我可以找到indexer等效的VB(int this [int row,int column])。在VB中定义索引器是否可能?如果可能的话,如何在VB中定义索引器?

Structure Matrix
  Private ReadOnly Rows, Columns As Integer
  Private _Matrix As Integer(,)

  Public Sub New(ByVal Rows As Integer, Columns As Integer)
    Me.Rows = Rows
    Me Columns = Columns
    ReDim _Matrix(Rows, Columns)
  End Sub

Public Sub New(ByVal RowsOrColumns As Integer)
    Me.Rows = RowsOrColumns
    Me Columns = RowsOrColumns
    ReDim _Matrix(RowsOrColumns, RowsOrColumns)
  End Sub
End Structure

还有一个问题!我的目的是指定_matrix(二维数组)的尺寸(矩阵(byval lows as Integer),列为整数)> code>> matrix(byval rowsorcolumns作为integer as integer as integer as integer )(构造方法方法)。我使用redim来完成。有没有更好的方法来做到这一点,还是我做得很好?

struct Matrix {
  readonly int Rows, Columns;
  int[,] _Matrix;

  public Matrix(int Rows, int Columns) {
    this.Rows = Rows;
    this.Columns = Columns;
    _Matrix = new int[Rows, Columns]
  }

  public Matrix(int RowsOrColumns) {
    this.Rows = RowsOrColumns;
    this.Columns = RowsOrColumns;
    _Matrix = new int[RowsOrColumns, RowsOrColumns]
  }

  public int this[int Row, int Column] {
    get => _Matrix[Row, Column];
    set => _Matrix[Row, Column] = value;
  }
}

I wrote a Matrix structure in C#. I was trying to write the same Matrix structure with VB but I can find VB equivalent of indexer(int this[int Row, int Column]). Is defining indexer in VB possible? How to define indexer in VB if it's possible?

Structure Matrix
  Private ReadOnly Rows, Columns As Integer
  Private _Matrix As Integer(,)

  Public Sub New(ByVal Rows As Integer, Columns As Integer)
    Me.Rows = Rows
    Me Columns = Columns
    ReDim _Matrix(Rows, Columns)
  End Sub

Public Sub New(ByVal RowsOrColumns As Integer)
    Me.Rows = RowsOrColumns
    Me Columns = RowsOrColumns
    ReDim _Matrix(RowsOrColumns, RowsOrColumns)
  End Sub
End Structure

One more question! I aim to specify the sizes of _Matrix(two-dimensional array) in Matrix(ByVal Rows As Integer, Columns As Integer) or Matrix(ByVal RowsOrColumns As Integer)(constructor methods). I use ReDim to do it. Is there a better way to do this or is my way good to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恬淡成诗 2025-02-19 16:34:31

c#索引器的VB等效属性是默认属性:

Default Public Property Item(ByVal Row As Integer, ByVal Column As Integer) As Integer
    Get
        Return _Matrix(Row, Column)
    End Get
    Set(ByVal value As Integer)
        _Matrix(Row, Column) = value
    End Set
End Property

另请参见:

The VB equivalent of a C# indexer is a Default Property:

Default Public Property Item(ByVal Row As Integer, ByVal Column As Integer) As Integer
    Get
        Return _Matrix(Row, Column)
    End Get
    Set(ByVal value As Integer)
        _Matrix(Row, Column) = value
    End Set
End Property

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文