Quick Start: Your First Table
This guide will walk you through creating a new console application, installing ConsoleTables, and displaying your first table.
Step 1: Create a New Console App
First, open your terminal or command prompt and create a new .NET console application.
dotnet new console -o MyFirstTableApp
cd MyFirstTableApp
Step 2: Install ConsoleTables
Next, add the ConsoleTables NuGet package to your new project.
dotnet add package ConsoleTables
Step 3: Write the Code
Now, open the Program.cs
file in your favorite editor and replace its contents with the following code:
using System;
using ConsoleTables;
namespace MyFirstTableApp
{
class Program
{
static void Main(string[] args)
{
// 1. Create a new ConsoleTable with column headers
var table = new ConsoleTable("Name", "Role", "Years of Service");
// 2. Add rows to the table
table.AddRow("Alice", "Developer", 5)
.AddRow("Bob", "Project Manager", 8)
.AddRow("Charlie", "UX Designer", 3);
// 3. Write the table to the console
table.Write();
Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
}
}
Code Breakdown:
new ConsoleTable(...)
: We instantiate aConsoleTable
object, passing the column headers as string arguments to the constructor..AddRow(...)
: We use the fluentAddRow
method to add data. Each argument corresponds to a column in the order they were defined. You can chainAddRow
calls together..Write()
: This method formats and prints the entire table to the standard console output.
Step 4: Run Your Application
Save the file and run your application from the terminal:
dotnet run
You should see the following output:
--------------------------------------------------------
| Name | Role | Years of Service |
--------------------------------------------------------
| Alice | Developer | 5 |
--------------------------------------------------------
| Bob | Project Manager | 8 |
--------------------------------------------------------
| Charlie | UX Designer | 3 |
--------------------------------------------------------
Count: 3
Press any key to exit...
Congratulations! You've created and displayed your first table with ConsoleTables. Explore the Core Concepts section to learn about more advanced features.