printing all contents of array in C#
You may try this:
foreach(var item in yourArray)
{
Console.WriteLine(item.ToString());
}
Also you may want to try something like this:
yourArray.ToList().ForEach(i => Console.WriteLine(i.ToString()));
EDIT: to get output in one line [based on your comment]:
Console.WriteLine("[{0}]", string.Join(", ", yourArray));
//output style: [8, 1, 8, 8, 4, 8, 6, 8, 8, 8]
EDIT(2019): As it is mentioned in other answers it is better to use Array.ForEach<T>
method and there is no need to do the ToList
step.
Array.ForEach(yourArray, Console.WriteLine);
C# Arrays - Printing multiple values in an array
I don't know exactly what the problem is. What I see is the following:
You create an array with 40 entries. Your first loop loops 41 times, so at the last position, you'll get an execption. The second loop starts by 1 and so, you are ignoring the first student. You should have the following code:
For the first loop:
for (int i = 0; i < testOne.Length; i++)
{
}
And for your second loop:
for (int i = 0; i < count; i++)
{
Console.WriteLine((i+1) + " " + testOne[i] + " " + testTwo[i]);
}
EDIT:
To format it somehow like in your example, you can work with number formats and with \t (tab). Please be aware that you should check if the input is an int before parsing it (I don't do this in the example here, for an easier explanation). If you would like to right align it, you need to check if the value is < 10, and the add spaces etc. But remember, it's a console app, so the output don't need to be perfect in each case :)
Console.WriteLine("\nStudent\tTest 1\tTest 2\tTotal");
Console.WriteLine("=======\t======\t======\t=====");
for (int i = 0; i < count; i++)
{
int one = int.Parse(testOne[i]);
int two = int.Parse(testTwo[i]);
Console.WriteLine((i + 1) + "\t" + one.ToString("0.0") + "\t" + two.ToString("0.0") + "\t" + (one + two).ToString("0.0"));
}
Or try what MAV mentioned in the comment :)
How do I print out Array of objects with a set of values in each element C#
This is because in your setInformation()
method, you're creating a new Seller
and setting it's values. When you get each property value you want to store it to this
. For example:
public void setInformation()
{
Console.WriteLine("Enter Name");
this.Name = Console.ReadLine();
...
}
You could also move the setInformation()
method outside the class and have it return the newly created Seller
. For example:
public static Seller SetInformation()
{
Seller seller = new Seller();
Console.WriteLine("Enter Name");
seller.Name = Console.ReadLine();
Console.WriteLine("Personal number");
seller.PersonalNumber = Console.ReadLine();
Console.WriteLine("District");
seller.District = Console.ReadLine();
Console.WriteLine("Sales");
seller.Sales = Console.ReadLine();
Console.WriteLine();
return seller;
}
Edit
It looks like you have a couple other issues in the code you've provided.
First off, you have inconsistent capitalization all over. the Seller
class is not capitalized (it should be) but in some places you use the capitalized version.
Next, the line Convert.ToInt32(seller.sales);
isn't needed and will fail because the property name should be capitalized.
The variable amount
is referenced but not set anywhere.
To summarize: Change the class name to Seller
as it should be. Remove that unnecessary line, and define amount
somewhere. With my these changes and my answer your code should run correctly.
Print arrays inside list
There're several ways:
Two loops:
foreach (string[] line in loggBoken) {
foreach(string item in line) {
Console.Write(item);
Console.Write('\t');
}
Console.WriteLine();
}
One loop, one Join
:
foreach (string[] line in loggBoken)
Console.WriteLine(string.Join("\t", line));
No loops, two Join
s (and Linq):
Console.Write(string.Join(Environment.NewLine, loggBoken
.Select(line => string.Join("\t", line))));
Related Topics
Conditional Operator Assignment with Nullable<Value> Types
How to Get Windows Display Settings
Making a Simple Ajax Call to Controller in ASP.NET MVC
How to Stop C# Console Applications from Closing Automatically
Parallel.Foreach and Async-Await
Set Timeout for Webclient.Downloadfile()
How to Create a Self-Signed Certificate Using C#
What Is the Correct Way to Use JSON.Net to Parse Stream of JSON Objects
Find Number of Decimal Places in Decimal Value Regardless of Culture
How to Return Null from a Generic Method in C#
Run Async Method Regularly with Specified Interval
Cast List<T> to List<Interface>
Conversion of a Datetime2 Data Type to a Datetime Data Type Results Out-Of-Range Value
Compiler Ambiguous Invocation Error - Anonymous Method and Method Group with Func<> or Action
Cross-Thread Operation Not Valid
How to Concatenate Two Arrays in C#
How Does the C# Compiler Detect Com Types
How to Correctly Implement a Backgroundworker with Progressbar Updates