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

我想创建一个带有约束的定制泛型向量类,即类型参数必须实现接口INumber(Of T).然而,当我试图在所述类中声明+-operator(使用由两个向量给出的值的元素相加)时,我的VB.NET项目产生了以下错误:

错误BC30452没有为类型‘T’和‘T’定义运算符‘+’

因为我几乎只测试了this page上给出的例子,并不真的相信微软会发布一些不起作用的东西,所以我决定也用C#(也使用.NET7)来测试我的问题.

为此,我在VB.NET年内建立了两个MWE

Vector.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 Sub SetValue(i As Integer, val As T)
        _items(i) = val
    End Sub

    Public Sub SetAll(val As T)
        For i As Integer = 0 To _items.Length - 1
            _items(i) = val
        Next
    End Sub

    Public Sub Print()
        For i As Integer = 0 To _items.Length - 1
            Console.WriteLine(_items(i))
        Next
    End Sub

    Public Shared Operator +(a As Vector(Of T), b As Vector(Of T)) As Vector(Of T)
        Dim retVal As New Vector(Of T)(a.Size)
        For i As Integer = 0 To retVal.Size - 1
            retVal.SetValue(i, a.GetValue(i) + b.GetValue(i))
        Next
        Return retVal
    End Operator

End Class

Program.vb:

Module Program
    Sub Main(args As String())
        Dim x As New Vector(Of Double)(3)
        x.SetAll(3)
        Dim y As New Vector(Of Double)(3)
        y.SetAll(2)
        Dim z = x + y
        z.Print()
    End Sub
End Module


C#年内

Vector.cs:

using System.Numerics;


namespace Test
{
    public class Vector<T> where T : INumber<T>
    {
        private T[] _items;

        public Vector(int n) {
            Array.Resize<T>(ref _items, n);
        }
        public int Size {
            get { return _items.Length; }
        }
        public T GetValue(int i) {
            return _items[i];
        }
        public void SetValue(int i, T val){
            _items[i] = val;
        }
        public void SetAll(T val) {
            for (var i = 0; i < _items.Length; i++) {
                _items[i] = val;
            }
        }
        public void Print() {
            for (var i = 0; i < _items.Length; i++) {
                Console.WriteLine(_items[i]);
            }
        }
        public static Vector<T> operator +(Vector<T> a, Vector<T> b)
        {
            var retVal = new Vector<T>(a.Size);
            for (var i = 0; i < retVal.Size; i++) {
                retVal.SetValue(i, a.GetValue(i) + b.GetValue(i));
            }
            return retVal;
        }
    }
}

Program.cs:

using Test;


var x = new Vector<Double>(3);
x.SetAll(3);
var y = new Vector<Double>(3);
y.SetAll(2);
var z = x + y;
z.Print();

在每个MWE中,创建长度为3的两个向量(双倍)x,y,使得x的所有条目都是3,y的所有条目都是2.然后将这些向量相加并保存在向量z中.

C#项目编译时没有出现错误,并生成以下输出

5
5
5

while the VB.NET project won't compile and throws above mentioned error.

I don't even really understand why the error is thrown in the first place. The type parameter was given the constrain, that it has to implement the interface INumber(Of T), which in return implements the IAdditionOperators(Of T,T,T) interface, which in return exposes the +-operator. I therefore thought, that T should be able to rely on said operator, like it does in C#, but not in VB.NET.

Is that an error on my site or is VB.NET not working correctly in that regard?

推荐答案

泛型数学是在C#11中引入的,其中was released in November 2022.

Visual Basic hasn't had a new version since 2019.目前,Microsoft的stated strategy for Visual Basic不会采用需要更改语言的功能:

我们将确保Visual Basic仍然是一种简单易懂的语言,具有稳定的设计..NET的核心库(如BCL)将支持VB,对.NET Runtime和库的许多改进将自动使VB受益.当C#或.NET运行时引入需要语言支持的新功能时,VB通常会采用仅消耗的方法并避免新的语法.我们不打算将Visual Basic扩展到新的工作负载.

因此,这只是一个C#拥有而Visual Basic没有的功能的例子.随着时间的推移,随着C#的不断发展和Visual Basic的"稳定",还会有更多这样的功能.

Csharp相关问答推荐

如何基于记录数组,在一次拍摄中更新单个MongoDB集合的多个记录中的子嵌入文档?

当我 for each 请求创建实例时,我是否应该在C#中部署httpClient?

ASP.NET Core -是否可以对所有最小API端点应用过滤器?

更新数据库中的对象失败,原因是:Microsoft. EntityFrame Core. GbUpdateConcurrencyResponse'

如何告诉自己创建的NuGet包在应用程序中发生了变化?

FileStream. FlushAsync()是否确保文件被写入磁盘?

在FilePath中搜索一个词,并根据First Match从左到右提取文件路径

从应用程序图API调用访问所有者字段

如何在Parall.ForEachAsync中使用CancerationTokenSource

如何使用C#中的主构造函数功能使用多个构造函数?

Automapper 12.x将GUID映射到字符串

用于管理System.Text.Json中的多态反序列化的自定义TypeInfoResolver

如果是,我怎么才能让这个加75,如果不是,我怎么才能减go 100?

如何在.NET MAUI中最大化GraphicsView的大小?

如何在特定环境中运行dotnet测试?

如何在使用Google.Drive.apis.V3下载文件/文件夹之前压缩?

在C#和HttpClient中使用REST API

SqlException:无法打开数据库.升级到Dotnet 8后-数据库兼容性版本-非EFCore兼容性级别

嵌套Blazor组件内的验证

.NET8支持Vector512,但为什么向量不能达到512位?