如何使用VB.NET IList(Of T).Max [英] How To Use VB.NET IList(Of T).Max

查看:115
本文介绍了如何使用VB.NET IList(Of T).Max的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 IList(Of T).Max 函数在我下面的示例中吗?

How do I use the IList(Of T).Max function in my example below?

Dim myList as IList(Of Integer)

For x = 1 to 10
    myList.add(x)
Next

'Error: 'Max' is not a member of 'System.Collections.Generic.IList(Of Integer)'
MsgBox(myList.Max()) 


推荐答案

您的代码在调用myList.add时会引发System.NullReferenceException,因为它尚未初始化。如果您使用List代替IList,如下所示,它将起作用。

your code throws a System.NullReferenceException when calling myList.add because it has not been initialized. If you use List instead of IList as shown below it works.

Imports System.Collections.Generic
Module Module1
    Sub Main()

        Dim myList As New List(Of Integer)

        For x = 1 To 10
            myList.Add(x)
        Next

        MsgBox(myList.Max())

    End Sub
End Module

即使在项目中仅导入系统,它也可以正常工作。

It works fine even if only System is imported in the project.

这篇关于如何使用VB.NET IList(Of T).Max的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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