Csharp/CSharp Tutorial/Development/Console Output

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Adds a total of 4 blank lines (then beep again!)

using System;
using System.Collections.Generic;
using System.Text;

  class Program
  {
    static void Main(string[] args)
    {
       Console.WriteLine("All finished.\n\n\n\a");
    }
  }

Concatenate string in Console.WriteLine

using System;
class MainClass{
    public static void Main()
    {
        Console.WriteLine("Hello " + "World");
    }
}
Hello World

Console I/O

Console I/O is accomplished through the standard streams

  1. Console.In,
  2. Console.Out, and
  3. Console.Error.
  1. Console.In is an instance of TextReader.
  2. Console.In defines two input methods: Read() and ReadLine().
  3. Console.Out and Console.Error are objects of type TextWriter.
  4. Console.Out and Console.Error are objects of type TextWriter.
  1. You can use the FileStream class to create a byte-oriented stream attached to a file.
  2. FileStream is derived from Stream.
  3. FileStream contains all of Stream"s functionality.

14.3.Console Output 14.3.1. Console I/O 14.3.2. <A href="/Tutorial/CSharp/0280__Development/WriteLine.htm">WriteLine</a> 14.3.3. <A href="/Tutorial/CSharp/0280__Development/Writewithoutnewline.htm">Write without new line</a> 14.3.4. <A href="/Tutorial/CSharp/0280__Development/SubstitutingValues.htm">Substituting Values</a> 14.3.5. <A href="/Tutorial/CSharp/0280__Development/SubstitutingValuestwice.htm">Substituting Values twice</a> 14.3.6. <A href="/Tutorial/CSharp/0280__Development/WritetoConsoleOutandConsoleError.htm">Write to Console.Out and Console.Error</a> 14.3.7. <A href="/Tutorial/CSharp/0280__Development/Outputaninttoconsole.htm">Output an int to console</a> 14.3.8. <A href="/Tutorial/CSharp/0280__Development/ConcatenatestringinConsoleWriteLine.htm">Concatenate string in Console.WriteLine</a> 14.3.9. <A href="/Tutorial/CSharp/0280__Development/Formatthesameargumentthreedifferentways.htm">Format the same argument three different ways</a> 14.3.10. <A href="/Tutorial/CSharp/0280__Development/Displayargumentsinnonsequentialorder.htm">Display arguments in non-sequential order</a> 14.3.11. <A href="/Tutorial/CSharp/0280__Development/Variableindex.htm">Variable index</a> 14.3.12. <A href="/Tutorial/CSharp/0280__Development/Variableindexreferenceavariablemorethanonce.htm">Variable index: reference a variable more than once</a> 14.3.13. <A href="/Tutorial/CSharp/0280__Development/EscapeCharacters.htm">Escape Characters: \\</a> 14.3.14. <A href="/Tutorial/CSharp/0280__Development/Addsatotalof4blanklinesthenbeepagain.htm">Adds a total of 4 blank lines (then beep again!)</a> 14.3.15. <A href="/Tutorial/CSharp/0280__Development/DisplayingaTriangleUsingaVerbatimStringLiteral.htm">Displaying a Triangle Using a Verbatim String Literal</a>

Display arguments in non-sequential order

using System;   
   
class MainClass {   
  public static void Main() {   
     
    Console.WriteLine("{2:d} {0:d} {1:d}", 1, 2, 3);   
   
  }   
}
3 1 2

Displaying a Triangle Using a Verbatim String Literal

class Triangle
{
static void Main()
  {
      System.Console.Write(@"begin
             /\
            /  \
           /    \
          /      \
         /________\
end");
  }
}

Escape Characters: \\

using System;
class MainClass
{
    static void Main(string[] args)
    {
        Console.WriteLine("Here comes a slash \\");
    }
}
Here comes a slash \

Format the same argument three different ways

using System;   
   
class MainClass {   
  public static void Main() {   
 
    Console.WriteLine("{0:F2}  {0:F3}  {0:e}", 10.12345);   
   
  }   
}
10.12  10.123  1.012345e+001

Output an int to console

using System;
class MainClass
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Value is: {0}", 3);
    }
}
Value is: 3

Substituting Values

using System;
class MainClass
{
   static void Main()
   {
      Console.WriteLine(" {0} and {1}.", 3, 6);
   }
}
3 and 6.

Substituting Values twice

using System;
class MainClass 
{
   static void Main() {
      Console.WriteLine("{1}, {0} and {1}.", 3, 6);
   }
}
6, 3 and 6.

Variable index

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;

public class MainClass
{
    public static void Main()
    {
            int age = 25;
            string name = "Jim";
            Console.WriteLine(String.Format("{0} is {1} years old.", name, age));
    }
}
Jim is 25 years old.

Variable index: reference a variable more than once

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
public class MainClass
{
    public static void Main()
    {
            string name = "Hello";
            Console.WriteLine(String.Format("{0} there. I said {0}! {0}???", name));
    }
}
Hello there. I said Hello! Hello???

WriteLine

class MainClass {
   static void Main()
   {
         System.Console.WriteLine("This is text 1.");
         System.Console.WriteLine("This is text 2.");
         System.Console.WriteLine("This is text 3.");
   }
}
This is text 1.
This is text 2.
This is text 3.

Write to Console.Out and Console.Error

using System; 
 
class MainClass { 
  public static void Main() { 
    int a=10, b=0; 
    int result; 
 
    Console.Out.WriteLine("This will generate an exception."); 
    try { 
      result = a / b; // generate an exception 
    } catch(DivideByZeroException exc) { 
      Console.Error.WriteLine(exc.Message); 
    } 
  } 
}
This will generate an exception.
Attempted to divide by zero.

Write without new line

Write( ) is used to display the string without a new line.


class MainClass {
   static void Main()
   {
         System.Console.Write("This is text 1.");
         System.Console.Write("This is text 2.");
         System.Console.Write("This is text 3.");
   }
}
This is text 1.This is text 2.This is text 3.