A C# star pattern is a console program that prints '*' in rows. The outer for loop chooses the row. The inner loop prints each star with Console.Write, then Console.WriteLine ends that row.
'*' + 1 is an int. The char is promoted before the addition, so Console.Write('*' + 1) writes 43, not another star. (char)('*' + 1) is +, the character whose code is 43. "*" + "*" is the string "**", because + concatenates when one operand is a string. "*" + 1 is "*1".
Console.Write('*') stays on the row.
It writes the character and does not end the line. Call Console.WriteLine() once after the inner loop. new string('*', n) builds a string of n stars, and WriteLine can print that whole row in one call.
Syntax
C# Syntax
The inner loop runs once per star. Write keeps those stars on one line, and WriteLine comes after that loop.
C#
for (int row = 1; row <= rows; row++)
{
for (int col = 1; col <= row; col++)
Console.Write('*'); // same row
Console.WriteLine(); // end the row
}
Reference
How C# Prints a Star
Map each part of the console shape to a C# value or call.
Piece
C#
What it does
Star
Console.Write('*')
Writes the character and stays on the row
Run of stars
new string('*', n)
A string of n stars
Indent
new string(' ', n)
n spaces
Char +
'*' + 1
The int 43
Cast
(char)('*' + 1)
The character +
String +
"*" + 1
The string "*1"
New row
Console.WriteLine()
Ends the row
Edge only
Write(col == 1 || col == width ? '*' : ' ')
A hollow row
Code
C# Examples
Three programs. Each one changes only the C# that chooses how many stars to print, and where the spaces go.
Example 1 — One star per inner-loop pass
Row 1 prints one star. Each later row prints one more, because the inner loop stops at row.
C#
using System;
class StarTriangle
{
static void Main()
{
int rows = 5;
for (int row = 1; row <= rows; row++)
{
for (int col = 1; col <= row; col++)
Console.Write('*');
Console.WriteLine();
}
}
}
Output
*
**
***
****
*****
How It Works
The outer loop walks row from 1 to 5.
Console.Write('*') writes the character and stays on the row.
Console.WriteLine() after the inner loop starts the next row.
Example 2 — Print the whole row with new string
Count row down from 5. One WriteLine writes every star and the newline.
C#
using System;
class StarRepeat
{
static void Main()
{
int rows = 5;
for (int row = rows; row >= 1; row--)
Console.WriteLine(new string('*', row));
}
}
Output
*****
****
***
**
*
How It Works
new string('*', row) fills a string with that many stars.
WriteLine writes that string and ends the row, so the inner loop is not needed.
row-- makes each later line one star shorter.
Example 3 — Join the spaces and the stars
new string(' ', n) builds the indent. new string('*', n) builds an odd run of stars. + joins the two strings.
C#
using System;
class StarPyramid
{
static void Main()
{
int rows = 5;
for (int row = 1; row <= rows; row++)
{
string spaces = new string(' ', rows - row);
string stars = new string('*', 2 * row - 1);
Console.WriteLine(spaces + stars);
}
}
}
Output
*
***
*****
*******
*********
How It Works
Row 1 builds 4 spaces and 1 star. Row 5 builds 0 spaces and 9 stars.
+ concatenates two strings. '*' + 1 would be the int 43.
WriteLine writes the joined line and ends the row.
Programs
11 C# Star Pattern Programs
Each card is a full C# program: nested for loops, Console.Write('*') or new string('*', n), and WriteLine. Open one for the walkthrough.
In C#, '*' + 1 is an int. The char is promoted before the addition, so the sum is 43 and Write uses the int overload. Console.Write('*') writes the character. (char)('*' + 1) is +, not a second star. "*" + 1 is the string "*1" because one operand is a string.
Console.Write('*') stays on the same row. Console.WriteLine() ends the row. Call WriteLine once after the inner loop. If WriteLine sits inside the inner loop, each star lands on its own line.
Yes, when the row is a solid run of stars. Console.WriteLine(new string('*', row)) writes that row and the newline in one call. Use the inner loop when the row mixes spaces and stars, or when only the edges should be stars.
Console.WriteLine(new string(' ', rows - row) + new string('*', 2 * row - 1)) builds the indent and the stars as one string. WriteLine adds the newline. Row 1 has one star; each later row adds two.