I am trying print 3 things in straight line like name, age and wages.
like this
Graham 47 500
Jess 47 250
Dave 23 100
type here
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace General_Employee_Data
{
class Program
{
static void Main(string[] args)
{
var Namelist = new string[2, 2, 3]
{
{ "Graham", "47", "500" },
{ "Jess" , "47", "250" },
{ "David", "23", "100" },
};
for (int i = 0; i < 3; i )
{
Console.WriteLine(Namelist[i, i, i]);
}
}
}
This isn't homework as I doing it to practise my C#, I seem made it more complex than simple.
I trying get arrays names, age , wages on the screen.
CodePudding user response:
To print an array on single line, either you can print each element one by one, like
Console.WriteLine(Namelist[i][0] "\t" Namelist[i][1] "\t" Namelist[i][2]);
Or you can use string.Join()
to convert string array to string,
Console.WriteLine(string.Join(@"\t", Namelist[i]));
I recommend creating a class instead of a string array to store all relevant fields.
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public int Wages { get; set; }
public Employee(string name, int age, int wages)
{
this.Name = name;
this.Age = age;
this.Wages = wages;
}
//This will help you to convert, Employee object to expected string
public override string ToString()
{
return $"{this.Name} \t {this.Age} \t {this.Wages}";
}
}
Now store all details in List<Employee>
, like
List<Employee> employees = new List<Employee>()
{
new Employee("Graham", 47, 500),
new Employee("Jess", 47, 250),
new Employee("Dave", 23, 100)
}
Now whenever you want to print the detail, try below
foreach(var employee in employees)
Console.WriteLine(employee);
To learn more about Classes, Constructor, ToString method read below links
CodePudding user response:
what about converting it to a string and than printing it out like
Console.WriteLine(Namelist[i].ToString());