Question What are these called!!??

jayware33

Member
Joined
Jan 15, 2010
Messages
13
Programming Experience
Beginner
was coding in a console app today when my tutor sent me a short 2 line bit of code ... all looked good Except these funny looking brackets ... what are they called? he said they are called placeholders ... but i have heard there is another name for them?? this is what they look like { } ....... they are not round like normal brackets so what are they?

here is the code

Dim test As String = "John"

Console.WriteLine("{0}", test)




thanks
 
anyone got any suggestions?
 
The bracket is a place holder for the String.Format() to fill in the "Blanks", being the values of the variables in order, separated by commas.

Console.WriteLine() calls String.Format() in the background which is why you don't actually see "String.Format()" in the code.

String.Format("{0} + {1} = {2}", "3", "2", "5") produces the output of "3 + 2 = 5"
 
The placeholder consists of a pair of curly braces enclosing a number beginning with 0 for the first expression, 1 for the second expression (if any), and so on. The placeholders are inside the text quotation. The expressions follow a comma after the end of the text. An expression may be either a literal string, a literal number, a variable, or a math operation. The order is important as the expressions must match the placeholder numbers. The placeholder numbers may be repeated to display the same expression more than once, but the number must match.

Here is a complete demo program you can study to see how placeholders work:


VB.NET:
Module Module1
    Sub Main()
        Dim snom, avg As String
        Console.WriteLine("The WriteLine method uses placeholders to hold a value.")
        Console.WriteLine("Placeholders start with 0 enclosed within curly braces.")
        Console.WriteLine("A value is placed after the string following a comma.")
        Console.WriteLine("The value replaces the string in the display.")
        Console.WriteLine() 'skips a line
        Console.ReadLine()  'waits for user to press Enter
        Console.WriteLine("Values may be strings, numbers, variables, or expressions.")
        Console.WriteLine("My name is {0}.", "George")
        Console.WriteLine("I passed the {0} test.", "math")
        Console.WriteLine("My average was {0}.", 85)
        Console.ReadLine()  'waits for user to press Enter
        Console.Clear()     'clears the screen
        Console.WriteLine("Number expressions that are not strings can be computed.")
        Console.WriteLine("The computed results are copied to the placeholders.")
        Console.WriteLine("5 + 3 is {0}.", "5 + 3")
        Console.WriteLine("5 + 3 is {0}.", 5 + 3)
        Console.WriteLine() 'skips a line
        Console.ReadLine()  'waits for user to press Enter
        Console.WriteLine("More than one placeholder can be used.")
        Console.WriteLine("The placeholders are numbered in order beginning with 0.")
        Console.WriteLine("There must be the same number of values following the comma.")
        Console.WriteLine("The values must be in the same order as the placeholder numbers.")
        Console.WriteLine() 'skips a line
        Console.WriteLine("{0} has an average of {1}.", "George", 85)
        Console.WriteLine("His test scores were {0}, {1}, and {2}.", 75, 92, 88)
        Console.WriteLine("His average is {0}.", ((75 + 92 + 88) / 3))  'uses an expression
        Console.WriteLine() 'skips a line
        Console.ReadLine()  'waits for user to press Enter
        Console.WriteLine("Placeholder values can be repeated by using the same number.")
        Console.WriteLine("The box has room for {0} items and I have all {0}.", 12)
        Console.WriteLine("The box has room for {0} {1} and I have all {0}.", 12, "eggs")
        Console.WriteLine() 'skips a line
        Console.ReadLine()  'waits for user to press Enter
        Console.WriteLine("Variables may be used with placeholders.")
        snom = "George"
        avg = "85"
        Console.WriteLine("{0} has an average of {1}.", snom, avg)
        Console.ReadLine()
    End Sub
End Module
 
Also used in arrays:
VB.NET:
Dim myIntegers() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
And other types as well.
 
Back
Top