Csharp/C Sharp/Development Class/Console

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

Beep

<source lang="csharp"> using System; using System.Media; class MainClass {

   public static void Main() {
       Console.Beep();
   }

}

</source>


Beep(

<source lang="csharp"> using System; using System.Media; class MainClass {

   public static void Main() {
       // Play a  beep with frequency as 200 and duration as 300
       Console.Beep(200, 300);
   }

}

</source>


Change console foreground color and background color

<source lang="csharp"> using System; public class Test{

  static void Main(string[] args){
     Console.Title = "Standard Console";
     Console.ForegroundColor = ConsoleColor.Red;
     Console.BackgroundColor = ConsoleColor.Green;
     Console.WriteLine("Press Enter to change the Console"s appearance.");
     Console.ReadLine();
 }

}


      </source>


Clear console

<source lang="csharp"> using System; public class Test{

  static void Main(string[] args){
     // Change the Console appearance and redisplay.
     Console.Title = "Cleared / Colored Console";
     Console.ForegroundColor = ConsoleColor.Blue;
     Console.BackgroundColor = ConsoleColor.Yellow;
     Console.Clear();
     Console.WriteLine("Press Enter to change the Console"s appearance.");
     Console.ReadLine();
 }

}


      </source>


CursorVisible ResetColor SetWindowSize BufferHeight BufferWidth CursorLeft CursorSize CursorTop

<source lang="csharp"> using System; public class MainClass {

   static void Main(string[] args) {
      // Change the console appearance and redisplay.
      Console.Title = "Resized Console";
      Console.ResetColor();
      Console.Clear();
      Console.SetWindowSize(100, 50);
      Console.BufferHeight = 500;
      Console.BufferWidth = 100;
      Console.CursorLeft = 20;
      Console.CursorSize = 50;
      Console.CursorTop = 20;
      Console.CursorVisible = false;
  }

}

</source>


Displaying the sum of two numbers input from the keyboard.

<source lang="csharp"> using System; public class Addition {

  public static void Main( string[] args )
  {
     int number1; 
     int number2; 
     int sum; 
     Console.Write( "Enter first integer: " ); 
     number1 = Convert.ToInt32( Console.ReadLine() );
     Console.Write( "Enter second integer: " );
     number2 = Convert.ToInt32( Console.ReadLine() );
     sum = number1 + number2;
     Console.WriteLine( "Sum is {0}", sum );
  } 

}


      </source>


Echo some stats

<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; class Program {

   static void Main(string[] args) {
       Console.Write("Enter your name: ");
       string s;
       s = Console.ReadLine();
       Console.WriteLine("Hello, {0} ", s);
       Console.Write("Enter your age: ");
       s = Console.ReadLine();
       Console.WriteLine("You are {0} years old", s);
   }

}

</source>


Printing multiple lines of text with string formatting.

<source lang="csharp">

using System; public class Welcome4 {

  public static void Main( string[] args )
  {
     Console.WriteLine( "{0}\n{1}", "Welcome to", "C# Programming!" );
  } 

}

      </source>


Printing multiple lines with a single statement.

<source lang="csharp">

using System; public class Welcome3 {

  public static void Main( string[] args )
  {
     Console.WriteLine( "Welcome\nto\nC#\nProgramming!" );
  } 

}

      </source>


Printing one line of text with multiple statements.

<source lang="csharp"> using System; public class Welcome2 {

   public static void Main(string[] args) {
       Console.Write("Welcome to ");
       Console.WriteLine("C# Programming!");
   }

}

      </source>


Read a line of string and check its length

<source lang="csharp"> using System; class MainEntryPoint {

   static void Main(string[] args) {
       Console.WriteLine("Type in a string");
       string input;
       input = Console.ReadLine();
       if (input == "") {
           Console.WriteLine("You typed in an empty string");
       } else if (input.Length < 5) {
           Console.WriteLine("The string had less than 5 characters");
       } else if (input.Length < 10) {
           Console.WriteLine("The string had at least 5 but less than 10 characters");
       }
       Console.WriteLine("The string was " + input);
   }

}

</source>


Set console: title, window size, buffer height and width, cursor position

<source lang="csharp"> using System; public class Test{

  static void Main(string[] args){
     // Change the Console appearance and redisplay.
     Console.Title = "Resized Console";
     Console.ResetColor();
     Console.Clear();
     Console.SetWindowSize(100, 50);
     Console.BufferHeight = 500;
     Console.BufferWidth = 100;
     Console.CursorLeft = 20;
     Console.CursorSize = 50;
     Console.CursorTop = 20;
     Console.CursorVisible = false;
     Console.WriteLine("Main method complete. Press Enter.");
     Console.ReadLine();
 }

}


      </source>


Use Red and Green from ConsoleColor

<source lang="csharp">

using System;

public class MainClass {

   static void Main(string[] args) {
      // Change the console appearance and redisplay.
      Console.Title = "Colored Text";
      Console.ForegroundColor = ConsoleColor.Red;
      Console.BackgroundColor = ConsoleColor.Green;
  }

}

      </source>


WriteLine and Write

<source lang="csharp">

using System; class ConsoleAdder {

   public static void Main() {
       int a = 15;
       int b = 744;
       int c = a + b;
       Console.Write("The sum of ");
       Console.Write(a);
       Console.Write(" and ");
       Console.Write(b);
       Console.Write(" equals ");
       Console.WriteLine(c);
       Console.WriteLine("The sum of " + a + " and " + b + " equals " + c);
       Console.WriteLine("The sum of {0} and {1} equals {2}", a, b, c);
   }

}

</source>