Console.WriteLine Need some help

Discussion in 'Mixed Languages' started by carmz, May 24, 2012.

  1. carmz

    carmz MDL Senior Member

    Nov 29, 2011
    285
    7
    10
    #1 carmz, May 24, 2012
    Last edited by a moderator: Apr 20, 2017
    [SOLVED] Console.WriteLine Need some help

    Hi,I need some help on this code,I have read some example in c# and this example i could not understand,is it okay to ask some help to explain on me or enlighten my mind on this,i am still beginner on this language.My question is why is this

    As i have know in java language
    System.out.println("{0} value: {1}");
    this will print {0} value: {1} //because it is inside in double quote.

    In c#

    {0} value: {1} is not treated as string?





    Code:
     int i = 20;
    float f = 20.2f;
    string s = “Hello, world...again”;
    Console.WriteLine(“This is an {0} value: {1}”, i.GetTypeCode(), i);
    Console.WriteLine(“This is a {0} value: {1}”, f.GetTypeCode(), f);
    Console.WriteLine(“This is a {0} value: {1}”, s.GetTypeCode(), s);
    
    
    

    Output:

    This is an Int32 Value: 20
    This is a Single Value: 20.2
    This is a String Value: Hello, world...again

    I hope you can help me Thank you so much in advance.
     
  2. jcgo16

    jcgo16 MDL Junior Member

    Sep 16, 2010
    74
    2
    0
    Console.WriteLine(“This is an " & {0} & " value: " & {1}, i.GetTypeCode(), i);
    Console.WriteLine(“This is a " & {0} & " value: " & {1}, f.GetTypeCode(), f);
    Console.WriteLine(“This is a " & {0} & " value: " & {1}, s.GetTypeCode(), s);

    im i doing this right?
     
  3. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,324
    60
    #3 QuantumBug, May 24, 2012
    Last edited by a moderator: Apr 20, 2017
    I know this sounds silly, but the code isn't recognising your quotes(""), replace them with Shift+2.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                 int i = 20;
    float f = 20.2f;
    string s = "Hello, world...again";
    System.Console.WriteLine("This is an {0} value: {1}", i.GetTypeCode(), i);
    System.Console.WriteLine("This is a {0} value: {1}", f.GetTypeCode(), f);
    System.Console.WriteLine("This is a {0} value: {1}", s.GetTypeCode(), s);
    System.Console.ReadLine(); //This code makes the console pause so you can read the output.
            }
    
        }
    }
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. carmz

    carmz MDL Senior Member

    Nov 29, 2011
    285
    7
    10
    #4 carmz, May 25, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Hi KM@Dave,
    Thank you for correcting the quotes, my problem is what is the purpose of this code
    {0} value: {1} why is it,it can display "This is an Int32"?
    the second line is "This is a Single".
    The third line is "This is a String" can you please help me enlighten my mind.

    Thank you in advance.
     
  5. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,324
    60
    You are printing a formatted string. The {0} means to insert the first parameter following the format string. The {1} is the additional parameter.

    Look here for more information.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. carmz

    carmz MDL Senior Member

    Nov 29, 2011
    285
    7
    10
    Hi KM@ Dave,thank you so much it helps me.
     
  7. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,324
    60
    Glad to hear that, I'll always be here to help with Development so give me a shout if you need help :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. carmz

    carmz MDL Senior Member

    Nov 29, 2011
    285
    7
    10
    Yes, i will.