Skip to main content
added 90 characters in body
Source Link
Blindman67
  • 22.9k
  • 2
  • 17
  • 40

Rather than send to the console as you build. Build a string then send to the console. This avoids some of the overhead associated with writing to IO.

In terms of performance you can use a System.Text.StringBuilder and avoid some of the allocation overhead of new String. StringBuilder will allocate new memory as the string grows, doubling the buffer each time it does so.

StringBuilder.Append can be used to add a number of char at a time reducing the code complexity.

You can write the built string directly to the console, or if needed convert it to a string via StringBuilder.ToString.

As a function, passing the pyramid size would make it more useful. As an example you can also use an optional argument to define what the pyramid is built of. Optional arguments require a default value

public static void ConsolePyramid(int size, char block = '#') {
    StringBuilder pyramid = new StringBuilder();
    for (int i = 0; i <= size; i++) {
        pyramid.Append(' ', size - i);
        pyramid.Append(block, i * 2 + 1);
        pyramid.Append(Environment.NewLine);
    }       
    Console.Write(pyramid);
}

Or

public static string BuildPyramidString(int size, char block = '#') {
    StringBuilder pyramid = new StringBuilder();
    for (int i = 0; i <= size; i++) {
        pyramid.Append(' ', size - i);
        pyramid.Append(block, i * 2 + 1);
        pyramid.Append(Environment.NewLine);
    }       
    return pyramid.ToString();
}

In terms of performance you can use a System.Text.StringBuilder and avoid some of the allocation overhead of new String. StringBuilder will allocate new memory as the string grows, doubling the buffer each time it does so.

StringBuilder.Append can be used to add a number of char at a time reducing the code complexity.

You can write the built string directly to the console, or if needed convert it to a string via StringBuilder.ToString.

As a function, passing the pyramid size would make it more useful. As an example you can also use an optional argument to define what the pyramid is built of. Optional arguments require a default value

public static void ConsolePyramid(int size, char block = '#') {
    StringBuilder pyramid = new StringBuilder();
    for (int i = 0; i <= size; i++) {
        pyramid.Append(' ', size - i);
        pyramid.Append(block, i * 2 + 1);
        pyramid.Append(Environment.NewLine);
    }       
    Console.Write(pyramid);
}

Or

public static string BuildPyramidString(int size, char block = '#') {
    StringBuilder pyramid = new StringBuilder();
    for (int i = 0; i <= size; i++) {
        pyramid.Append(' ', size - i);
        pyramid.Append(block, i * 2 + 1);
        pyramid.Append(Environment.NewLine);
    }       
    return pyramid.ToString();
}

Rather than send to the console as you build. Build a string then send to the console. This avoids some of the overhead associated with writing to IO.

In terms of performance you can use a System.Text.StringBuilder and avoid some of the allocation overhead of new String. StringBuilder will allocate new memory as the string grows, doubling the buffer each time it does so.

StringBuilder.Append can be used to add a number of char at a time reducing the code complexity.

You can write the built string directly to the console, or if needed convert it to a string via StringBuilder.ToString.

As a function, passing the pyramid size would make it more useful. As an example you can also use an optional argument to define what the pyramid is built of. Optional arguments require a default value

public static void ConsolePyramid(int size, char block = '#') {
    StringBuilder pyramid = new StringBuilder();
    for (int i = 0; i <= size; i++) {
        pyramid.Append(' ', size - i);
        pyramid.Append(block, i * 2 + 1);
        pyramid.Append(Environment.NewLine);
    }       
    Console.Write(pyramid);
}

Or

public static string BuildPyramidString(int size, char block = '#') {
    StringBuilder pyramid = new StringBuilder();
    for (int i = 0; i <= size; i++) {
        pyramid.Append(' ', size - i);
        pyramid.Append(block, i * 2 + 1);
        pyramid.Append(Environment.NewLine);
    }       
    return pyramid.ToString();
}
Source Link
Blindman67
  • 22.9k
  • 2
  • 17
  • 40

In terms of performance you can use a System.Text.StringBuilder and avoid some of the allocation overhead of new String. StringBuilder will allocate new memory as the string grows, doubling the buffer each time it does so.

StringBuilder.Append can be used to add a number of char at a time reducing the code complexity.

You can write the built string directly to the console, or if needed convert it to a string via StringBuilder.ToString.

As a function, passing the pyramid size would make it more useful. As an example you can also use an optional argument to define what the pyramid is built of. Optional arguments require a default value

public static void ConsolePyramid(int size, char block = '#') {
    StringBuilder pyramid = new StringBuilder();
    for (int i = 0; i <= size; i++) {
        pyramid.Append(' ', size - i);
        pyramid.Append(block, i * 2 + 1);
        pyramid.Append(Environment.NewLine);
    }       
    Console.Write(pyramid);
}

Or

public static string BuildPyramidString(int size, char block = '#') {
    StringBuilder pyramid = new StringBuilder();
    for (int i = 0; i <= size; i++) {
        pyramid.Append(' ', size - i);
        pyramid.Append(block, i * 2 + 1);
        pyramid.Append(Environment.NewLine);
    }       
    return pyramid.ToString();
}