Hi, I'd like to have tabs in my console app, but instead of using \t, I'd like to use an equivalent of the C++ setw() method in C#. Any ideas of what I might use?

And another thing, in C++, you would declare more than one array with indexes like this:

int x[10], y[10], z[10];

Is there a shorter way than writing like this in C# :

int[] x = new int[10];
int[] y = new int[10];
int[] z = new int[10];

Thanx for the help, if someone knows.

Edit: Oh, damnit, wrong forum! Can a mod move it to the C# forum?

Dani AI

Generated

For setw-style columns in a console, use .NET’s composite formatting (also available via string interpolation). It lets you specify a field width and alignment, which is more predictable than tabs. Example:

var items = new[] {
    new { Name = "apple", Qty = 3, Price = 1.25m },
    new { Name = "banana", Qty = 12, Price = 0.45m }
};

Console.WriteLine($"{"Item",-10} {"Qty",5} {"Price",8}");
foreach (var it in items)
    Console.WriteLine($"{it.Name,-10} {it.Qty,5} {it.Price,8:C}");

Notes:

  • Positive width right-aligns, negative left-aligns.
  • For zero-padding numbers, use a precision specifier like $"{n:D5}".
  • If columns must start at exact positions regardless of prior output, you can set Console.CursorLeft before writing, but formatting is usually simpler.

On arrays, ’s jagged suggestion is good if you want to manage a group together. But if you truly need three independent arrays, avoid the chained assignment idea from : assigning x = y = z = new int[10]; (after declarations) would make all three variables reference the same array instance. Also, int[] x, y, z = new int[10]; initializes only z; x and y would still be null. The safe, explicit approach is:

const int n = 10;
int[] x = new int[n];
int[] y = new int[n];
int[] z = new int[n];

If you want to keep them together without losing independence, you can group them:

const int n = 10;
var arrays = new int[3][];
for (int i = 0; i < arrays.Length; i++) arrays[i] = new int[n];
// arrays[0], arrays[1], arrays[2] are separate int[] of length n

That keeps your code DRY while ensuring each array is distinct.

Recommended Answers

All 4 Replies

Hey there silkfire,

On the array question, do you need three distinct arrays? You could create a multidimensional array, like a jagged array:

int [][][] njaggedArray = new int [10][10][10];

Will that work?

nah, wood like to separate the arrays, but thanx anyway =)

int[] x = y = z = new int[10];
// or I THINK this works too
int[] x, y, z = new int[10];

setw() equiv would be sth like this:

String.Format("{0,10:D}", 2) : will format number 2 as a string of width 10 aligned to the right, use -10 to have it aligned on the left

as for declaring the vars, you can do it like this:

int[] a, b, c = new int[10];

commented: Good info +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.