Program that prints an ASCII pyramid I have written the following program in C# to display an ASCII pyramid.

public static void method1()
{
  int k;
  int kCond = 5;
  int i;
  int inc = 0;
  for (int p = 0; p &lt;=5; p++)
  {
    for(k = 0; k &lt;= kCond; k++)
    {
         Console.Write(" ");
    }
     kCond--;
     for(i=0; i&lt;=inc; i++)
     {
        Console.Write("*")
     }
     inc +=2;
     Console.WriteLine();
  }
}


Output:

         *
        ***
       *****
      *******
     *********
    ***********


My question is, is this good code from a performance point of view, or not? How could I improve it?
I took your code and started condensing it, and came up with the following improvements that could be made:


Change the inner loops to use the string constructor that takes a character and a count instead of using a loop
Give the method and variables more meaningful names
Add a size parameter that can be passed to the method
Put declarations and incrementing of variables in the for statement
Use math operations on the counter rather than tracking multiple variables that increment at constant rates relative to the counter
Use Enumerable.Range as a looping mechanism to select the strings along with string.Concat to join them together
Have the method return a string so it can be written by the client to other things (like a log file or another control)


Code:

public static string GetPyramid(int size)
{
    return string.Concat(Enumerable.Range(0, size).Select(count =&gt;
        new string(' ', size - count) + 
        new string('*', count * 2 + 1) + 
        Environment.NewLine));
}