小编Geo*_*ald的帖子

为什么 string.join 在 VB.Net 中返回列表对象

我无法理解这两个命令之间的区别,在我看来这两个命令应该做同样的事情。我已经在下面发布了完整的代码,以防有任何不清楚的地方。

我在 Person 类中创建了两个函数,一个返回包含名字、中间名和姓氏的列表,另一个返回名称的串联字符串。我引用返回列表的函数来将字符串与下面的行连接起来:

FullName = String.Join(" ", Me.Get_NameList())
Run Code Online (Sandbox Code Playgroud)

但是,当我打电话时:

Console.WriteLine(Person1.Print_Name())
Run Code Online (Sandbox Code Playgroud)

我得到的看起来像列表对象而不是字符串:

System.Collections.Generic.List`1[System.String]
Run Code Online (Sandbox Code Playgroud)

如果我将代码更改为如下所示:

    Public Function Print_Name()
        Dim FullNameList As List(Of String) = Me.Get_NameList()
        Dim FullName As String
        FullName = String.Join(" ", FullNameList)
        Return FullName
    End Function
Run Code Online (Sandbox Code Playgroud)

控制台打印:

John Q Doe
Run Code Online (Sandbox Code Playgroud)

为什么我首先将列表分配给变量然后加入它会得到不同的答案?这与列表在内存中的存储方式有关系吗?

先谢谢您的帮助。

这是完整的代码:

Imports System
Module Module1
    Sub Main()
        Dim Person1 As New Person("John", "Q", "Doe")
        Console.WriteLine("Get_Name Values")
        Dim g1 As List(Of String) = Person1.Get_NameList()
        Console.WriteLine(String.Join(" ", g1))
        Console.WriteLine("Print_Name Values")
        Console.WriteLine(Person1.Print_Name())
    End Sub
End Module

Class Person
    Private FirstName …
Run Code Online (Sandbox Code Playgroud)

vb.net string list

3
推荐指数
1
解决办法
2283
查看次数

标签 统计

list ×1

string ×1

vb.net ×1