4

Please be easy on me guys as I'm still learning.

The following code:

Imports System.Console

Module Module1

    Sub Main()
        Dim num As Integer
        Dim name As String

        num = 1
        name = "John"

        WriteLine("Hello, {0}", num)
        WriteLine("Hello, {0}", name)
        WriteLine("Hello, {0}", 1)
        WriteLine("Hello, {0}", "John")
        WriteLine("5 + 5 = {0}", 5 + 5)

        WriteLine()
    End Sub

End Module

has the same output as this code:

Imports System.Console

    Module Module1

        Sub Main()
            Dim num As Integer
            Dim name As String

            num = 1
            name = "John"

            WriteLine("Hello, " & num)
            WriteLine("Hello, " & name)
            WriteLine("Hello, " & 1)
            WriteLine("Hello, " & "John")
            WriteLine("5 + 5 = " & 5 + 5)

            WriteLine()
        End Sub

    End Module

Both output:

Hello, 1
Hello, John
Hello, 1
Hello, John
5 + 5 = 10

I looked everywhere and couldn't find the answer.
When to use "{0}, {1}, ... etc"? and when to use "&"?
Which is better? And why?

Cœur
  • 37,241
  • 25
  • 195
  • 267
XO39
  • 481
  • 2
  • 9
  • 22
  • 1
    concatenating two strings, vs variable replacement in a string at any given point. – Mike McMahon Apr 09 '12 at 20:59
  • 2
    [This post][1] has some good answers too (in a C# context). [1]: http://stackoverflow.com/questions/296978/when-is-it-better-to-use-string-format-vs-string-concatenation – dizzwave Apr 09 '12 at 21:03

4 Answers4

6

With {0} you're specifying a format placeholder, whereas with & you're just concatenating the string.

Using format placeholders

Dim name As String = String.Format("{0} {1}", "James", "Johnson")

Using string concatenation

Dim name As String = "James" & " " & "Johnson"
James Johnson
  • 45,496
  • 8
  • 73
  • 110
5

What you're seeing here are two very different expressions that just so happen to evaluate to the same output.

The & operator in VB.Net is the string concatenation operator. It essentially works by converting both the left and right side of the expression to a String and them adding them together. This means all the below operations are roughly equivalent

"Hello " & num
"Hello " & num.ToString()
"Hello " & CStr(num)

The {0} is a feature of the .Net APIs. It represents a position within a string which will later be replaced with a value. The {0} refers to the first value passed to the function, {1} the second and so on. This means that all the below operations are roughly equivalent

Console.WriteLine("Hello {0}!", num)
Console.WriteLine("Hello " & num & "!")

The reason you see the same output is because putting {0} at the end of the string is almost exactly the same as a string concatenation of the 2 values.

James Johnson
  • 45,496
  • 8
  • 73
  • 110
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
4

Using {N} is called Composite Formatting. One advantage, besides readability, is the fact that you can easily set alignment and format properties. Example from the MSDN link:

Dim MyInt As Integer = 100
Console.WriteLine("{0:C}", MyInt)
' The example displays the following output
' if en-US is the current culture:
'        $100.00
keyboardP
  • 68,824
  • 13
  • 156
  • 205
2

{0} is a placeholder that is used in conjunction with String.Format in order to have a more readable and performant string substitutions. Several method calls, including WriteLine, have implicit calls to String.Format.

The problem with using concatenation is that each concat operation will create a new string, which consumes memory.

If you are performing a lot of substitutions, then the best performance will be to use System.Text.StringBuilder instead.

competent_tech
  • 44,465
  • 11
  • 90
  • 113