How To Use VB.NET IList(Of T).Max

此生再无相见时 提交于 2019-12-13 02:32:24

问题


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()) 

回答1:


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.




回答2:


You have to make sure you import System.Linq, and add the System.Core.dll as a reference to your project.

This is because Max is an extension method defined in System.Linq.Enumerable class. It is not defined in System.Collections.Generic.IList(Of T) interface.



来源:https://stackoverflow.com/questions/6630701/how-to-use-vb-net-ilistof-t-max

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!