none
Whats the difference between write and writeline?

    Question

  •  

    sw.Write(

     

     

     

     

    DateTime.Now.ToString()+" ==> "+"First Time The Log File Was Created"+Environment.NewLine);

     

    sw.WriteLine();

    I want that after the text "First Time Log..." Is show up there will be an empty line after space so the next line will start in the next line and not right after this one.

    So should i use Environment.NewLine or make WriteLine(); ?

    Cuz both giving me the same result wich is what i wanted i just wonder what should i use? And if its WirteLine() then what is the Environment.NewLine for? Or if i should use Environment.NewLine then when should i use the WriteLine() ?

     

    Thanks.


    danieli
    Sunday, December 26, 2010 10:11 PM

Answers

  • Hopefully this will shed some light:

    //File->New->Project->Visual C#->Windows->Console Application
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication2
    {
      class Program
      {
        static void Main( string[] args )
        {
          Console.WriteLine( "NewLine: {0} first line{0} second line{0} third line",
                 Environment.NewLine );
          
          Console.WriteLine( "NewLine: "+Environment.NewLine
                  +" first line"+Environment.NewLine
                  + " second line" + Environment.NewLine 
                  +" third line");
    
          Console.WriteLine( "NewLine: ");
          Console.WriteLine( " first line");
          Console.WriteLine(" second line");
          Console.WriteLine( " third line" );
    
          Console.Write( "Done, press Enter" );
          Console.ReadLine();
        }
      }
    }
    
    

    Output:

    NewLine:
      first line
      second line
      third line
    NewLine:
      first line
      second line
      third line
    NewLine:
      first line
      second line
      third line
    Done, press Enter

     


    what is you talking aboot?
    • Marked as answer by chocolade Monday, December 27, 2010 2:16 AM
    Sunday, December 26, 2010 11:19 PM
  • Write procedure writes a text and places the caret to after the last character in the text.

    (Console.Write("Thank you") -

    Output: Thank you)

    WriteLine rocedure writes a text and places the caret to the next line(like pressing enter in MS WORD)

    (Console.WriteLine("Thank you") -

    Output: Thank you

    | -> caret is placed here.

    )

    Environment.NewLine forces the caret to to a new line.(like \n in programming languages)

    (Console.Write("Thank" + Environment.NewLine + "you") -

    Output: Thank

    you)


    The more I study, the more I realize how little I know.
    • Marked as answer by chocolade Monday, December 27, 2010 2:16 AM
    Sunday, December 26, 2010 11:13 PM
  • It probably comes from .NET's VB heritage Print:

          for (int i = 0; i < 10; i++)
          {
            
    for (int j = 0; j < 10; j++)
              
    Console.Write(j.ToString() + " ");
            
    Console.WriteLine();
          }

    • Marked as answer by chocolade Monday, December 27, 2010 2:16 AM
    Sunday, December 26, 2010 11:35 PM

All replies

  • Use Write when you want to write some text.  Use WriteLine when you want to write some text and start a new line.  Using Write you can continue writing on the same line with subsequent Write(s).
    • Proposed as answer by Linguisht Thursday, June 12, 2014 2:07 PM
    Sunday, December 26, 2010 10:24 PM
  • Write procedure writes a text and places the caret to after the last character in the text.

    (Console.Write("Thank you") -

    Output: Thank you)

    WriteLine rocedure writes a text and places the caret to the next line(like pressing enter in MS WORD)

    (Console.WriteLine("Thank you") -

    Output: Thank you

    | -> caret is placed here.

    )

    Environment.NewLine forces the caret to to a new line.(like \n in programming languages)

    (Console.Write("Thank" + Environment.NewLine + "you") -

    Output: Thank

    you)


    The more I study, the more I realize how little I know.
    • Marked as answer by chocolade Monday, December 27, 2010 2:16 AM
    Sunday, December 26, 2010 11:13 PM
  • Hopefully this will shed some light:

    //File->New->Project->Visual C#->Windows->Console Application
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication2
    {
      class Program
      {
        static void Main( string[] args )
        {
          Console.WriteLine( "NewLine: {0} first line{0} second line{0} third line",
                 Environment.NewLine );
          
          Console.WriteLine( "NewLine: "+Environment.NewLine
                  +" first line"+Environment.NewLine
                  + " second line" + Environment.NewLine 
                  +" third line");
    
          Console.WriteLine( "NewLine: ");
          Console.WriteLine( " first line");
          Console.WriteLine(" second line");
          Console.WriteLine( " third line" );
    
          Console.Write( "Done, press Enter" );
          Console.ReadLine();
        }
      }
    }
    
    

    Output:

    NewLine:
      first line
      second line
      third line
    NewLine:
      first line
      second line
      third line
    NewLine:
      first line
      second line
      third line
    Done, press Enter

     


    what is you talking aboot?
    • Marked as answer by chocolade Monday, December 27, 2010 2:16 AM
    Sunday, December 26, 2010 11:19 PM
  • It probably comes from .NET's VB heritage Print:

          for (int i = 0; i < 10; i++)
          {
            
    for (int j = 0; j < 10; j++)
              
    Console.Write(j.ToString() + " ");
            
    Console.WriteLine();
          }

    • Marked as answer by chocolade Monday, December 27, 2010 2:16 AM
    Sunday, December 26, 2010 11:35 PM
  • but what does {0} mean without environmet.newline? I mean it just use as shortcut?

    OmIdNoUrI

    Tuesday, June 19, 2012 1:33 PM
  • but what does {0} mean without environmet.newline? I mean it just use as shortcut?

    OmIdNoUrI

    See the help for string.Format(). "{0}" is a placeholder that shows which of the following arguments (in this case, the 0th, i.e. first) should be formatted into the output string at that location.
    Tuesday, June 19, 2012 1:45 PM