C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Part 1: Here we print an int to the screen with Console.WriteLine. A newline comes after the int.
Part 2: We pass a string variable to WriteLine to print the string. This is another overload of the WriteLine static method.
OverloadStaticPart 3: Other types, like bool, can also be passed to Console.WriteLine. Even objects can be used.
C# program that uses Console.WriteLine
using System;
class Program
{
static void Main()
{
// Part 1: write an int with Console.WriteLine.
int valueInt = 4;
Console.WriteLine(valueInt);
// Part 2: write a string with the method.
string valueString = "Your string";
Console.WriteLine(valueString);
// Part 3: write a bool with the method.
bool valueBool = false;
Console.WriteLine(valueBool);
}
}
Output
4
Your string
False
C# program that uses no arguments
using System;
class Program
{
static void Main()
{
Console.WriteLine("A");
Console.WriteLine(); // Empty line.
Console.WriteLine("B");
}
}
Output
A
B
Warning: This will create a string temporary for the concatenation, but overall this rarely impacts performance.
C# program that uses concat and WriteLine
using System;
class Program
{
static void Main()
{
string name = "USER";
int id = 100;
// A string is created before WriteLine is called.
Console.WriteLine(name + ": " + id);
}
}
Output
USER: 100
Info: No string temporary is created to write the 3 parts, but 3 Console calls may be slower overall than a single string concatenation.
C# program that uses Write, WriteLine
using System;
class Program
{
static void Main()
{
string name = "USER";
int id = 100;
// Write 3 parts to the same line.
Console.Write(name);
Console.Write(": ");
Console.WriteLine(id);
}
}
Output
USER: 100
Tip: The commas in the format string are printed unchanged. Start counting at 0 for the format string substitution markers.
C# program that uses Console.WriteLine with format strings
using System;
class Program
{
static void Main()
{
string value1 = "Dot";
string value2 = "Net";
string value3 = "Perls";
Console.WriteLine("{0}, {1}, {2}",
value1,
value2,
value3);
}
}
Output
Dot, Net, Perls
Next: This example first writes the 4 chars in the array to the screen. It writes the middle 2 chars.
Char ArrayC# program that uses Console.WriteLine with arrays
using System;
class Program
{
static void Main()
{
char[] array = new char[] { 'a', 'b', 'c', 'd' };
// ... Write the entire char array on a line.
Console.WriteLine(array);
// ... Write the middle 2 characters on a line.
Console.WriteLine(array, 1, 2);
}
}
Output
abcd
bc
C# program that uses ToString with Console.WriteLine
using System;
class Test
{
public override string ToString()
{
// Printed by Console.WriteLine.
return "Test object string";
}
}
class Program
{
static void Main()
{
// Create class with ToString method.
Test test = new Test();
// WriteLine calls to the ToString method.
Console.WriteLine(test);
}
}
Output
Test object string
And: We then just call WriteLine() instead of Console.WriteLine. This is syntactic sugar—the program's instructions are the same.
C# program that shows using static syntax
using static System.Console;
class Program
{
static void Main()
{
// We can just write WriteLine instead of Console.WriteLine.
// ... This saves exactly 8 characters.
WriteLine("Hello my friend!");
}
}
Output
Hello my friend!
Tip: Console.Title allows us to change the console window title. We can use it to reduce the amount of text written to the screen.
C# program that uses Title property
using System;
class Program
{
static void Main()
{
while (true)
{
// Assign Console.Title property to string returned by ReadLine.
Console.Title = Console.ReadLine();
}
}
}
Tip: If a program is requiring a password, we could print an error message if caps lock is pressed and the password is incorrect.
Tip 2: We could even add a separate "mode" in a program depending on whether caps lock is pressed.
C# program that uses CapsLock property
using System;
using System.Threading;
class Program
{
static void Main()
{
while (true)
{
Thread.Sleep(1000);
bool capsLock = Console.CapsLock;
Console.WriteLine(capsLock);
}
}
}
Output
False
False
True
True
True
False
True
True
Tip: We cannot set the value of the NumberLock property. If we do not want the key pressed, we must tell the user to press it again.
C# program that uses NumberLock
using System;
using System.Threading;
class Program
{
static void Main()
{
while (true)
{
Console.WriteLine(Console.NumberLock);
Thread.Sleep(1000);
}
}
}
Output
False
False
True
True
False
False
Tip: With WindowHeight and its companion property LargestWindowHeight, we gain control over a window's height.
Console.WindowHeightC# program that sets height, width
using System;
class Program
{
static void Main()
{
// ... Width is the number of columns of text.
Console.WindowWidth = 40;
// ... Height is the number of lines of text.
Console.WindowHeight = 10;
// ... Say hello.
Console.WriteLine("Hi");
}
}
Version 1: This version calls Console.WriteLine 100 times. Each line has a single char (not including a newline).
Version 2: This version uses StringBuilder.AppendLine to merge 100 lines into a single string, and then calls Console.WriteLine once.
Result: It is about 10 times faster to only call Console.WriteLine once. Combining strings before writing them is faster.
Tip: To run this benchmark, change the value of the "version" int from 0 to 1 (and back again).
C# program that times Console.WriteLine
using System;
using System.Diagnostics;
using System.Text;
class Program
{
static void Main()
{
int version = 1;
var t1 = Stopwatch.StartNew();
if (version == 1)
{
// Version 1: write 100 separate lines.
for (int i = 0; i < 100; i++)
{
Console.WriteLine("x");
}
}
else if (version == 2)
{
// Version 2: write 100 lines as a single string.
StringBuilder temp = new StringBuilder();
for (int i = 0; i < 100; i++)
{
temp.AppendLine("x");
}
Console.WriteLine(temp.ToString());
}
// Results.
Console.WriteLine("TIME FOR VERSION {0}: {1} ms", version, t1.ElapsedMilliseconds);
}
}
Output
TIME FOR VERSION 1: 35 ms
TIME FOR VERSION 2: 3 ms