XO39
XO39

Reputation: 483

What's the differences between "{0}" and "&" in VB.NET?

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?

Upvotes: 4

Views: 19982

Answers (4)

JaredPar
JaredPar

Reputation: 754725

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.

Upvotes: 5

keyboardP
keyboardP

Reputation: 69372

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

Upvotes: 4

competent_tech
competent_tech

Reputation: 44931

{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.

Upvotes: 2

James Johnson
James Johnson

Reputation: 46047

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"

Upvotes: 6

Related Questions