小编Had*_*fel的帖子

为什么 C# 允许在具有约束 INumber 的泛型类中使用 + 运算符,而 VB.NET 不允许?

首先,我目前正在使用 VB.NET,因此对 C# 并不了解。

我想创建一个带有约束的自定义泛型 Vector 类,即类型参数必须实现接口INumber(Of T)。但是,当我尝试在所述类中声明+ 运算符(使用两个向量给定值的元素级加法)时,我的 VB.NET 项目产生了以下错误:

错误 BC30452 未为类型“T”和“T”定义运算符“+”

由于我几乎只测试了本页给出的示例,并且并不真正相信 Microsoft 会发布一些不起作用的东西,因此我决定也在 C# 中测试我的问题(也使用 .NET 7)。

为此,我在VB.NET中构建了以下两个 MWE

矢量.vb:

Imports System.Numerics


Public Class Vector(Of T As INumber(Of T))

    Private _items() As T

    Public Sub New(n As Integer)
        ReDim _items(n - 1)
    End Sub

    Public ReadOnly Property Size As Integer
        Get
            Return _items.Length
        End Get
    End Property

    Public Function GetValue(i As Integer) As T
        Return _items(i)
    End Function

    Public …
Run Code Online (Sandbox Code Playgroud)

c# vb.net .net-7.0

6
推荐指数
1
解决办法
103
查看次数

标签 统计

.net-7.0 ×1

c# ×1

vb.net ×1