This question already has answers here:
System.Int32[] displaying instead of Array elements [duplicate]
(4 answers)
Closed 2 years ago.
When I make a function to return an array with the correct results.
Instead of giving me the correct results, I get as result System.Int32[].
Anyone an idea why this is?
class Program
{
static void Main(string[] args)
{
Console.WriteLine(MultiplyByLength(new int[] {2,3,1,0}));
}
public static int[] MultiplyByLength(int[] arr)
{
return arr.Select(x => x * arr.Length).ToArray();
}
}
You need to format it some how. An array doesn't have a ToString() override that knows how you want to format your type (int[]) to a string, in such cases it just returns the type name (which is what you are seeing)
foreach(var item in MultiplyByLength(new int[] {2,3,1,0})
Console.WriteLine(item);
or
Console.WriteLine(string.Join(Environment.NewLine, MultiplyByLength(new int[] {2,3,1,0}));
or
Console.WriteLine(string.Join(",", MultiplyByLength(new int[] {2,3,1,0}));
Related
This question already has answers here:
Console.WriteLine and generic List
(9 answers)
Closed 5 years ago.
I'm trying to make the code will reverse the order of a list from 'Monday, Tuesday ,Wednesday ,Thursday, Friday' to 'Friday, Thursday, Wednesday, Tuesday, Monday'. Whenever I run the code all that appears is
'System.Collections.Generic.List'1[System.String]'
I think the problem may be that I have declared the elements in the list wrong.
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string> (new string[]{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"});
list.Reverse();
Console.WriteLine(list);
Console.ReadLine();
}
}
change
Console.WriteLine(list);
by
foreach(string item in list)
{
Console.WriteLine(item);
}
You need to iterate the list. Here you have a complete example of the reverse method: https://msdn.microsoft.com/es-es/library/b0axc2h2(v=vs.110).aspx
You need to tell the compiler how to convert this list to a string. The default ToString functionality for complex types is to output the name of the type.
What you can do is join the list before outputting it:
Console.WriteLine(string.Join(", ", list));
But it all depends on the exact output you are expecting.
This question already has answers here:
Return multiple values to a method caller
(28 answers)
Closed 5 years ago.
I want to define a function with two outputs. The first one is a boolean variable and the second one is 2D array with unknown numbers of rows and columns but the array will be defined if the boolean variable is true and if the boolean variable is false, the array is not defined. how can I define this function? I am thankful if anybody can exemplify it in an example.
Thanks
You want something like this .
Tuple<string, int> NameAndId()
{
// This method returns multiple values.
return new Tuple<string, int>("Test", 100);
}
Why not return null if array is not defined?
public static bool MyMethod(out int[,] array) {
array = null;
...
}
....
int[,] data;
if (MyMethod(out data)) {
....
}
Or in case of C# 7.0+
if (MyMethod(out var data)) {
....
}
Edit: if you want to return an array, but you don't know its Length (or want to adjust it) you can try working with List<T> and put .ToArray() in the end:
using System.Linq;
...
List<int> list = new List<int>();
list.Add(1);
list.Add(5);
list.Add(10);
...
list.Remove(5);
...
list.RemoveAt(0);
...
array = list.ToArray();
This question already has answers here:
printing all contents of array in C#
(13 answers)
How does the .ToString() method work?
(4 answers)
Closed 6 years ago.
I am using methods in c#. i returned something but compiler is not printing which i am expecting. It is printing (system.string[]). i dont know why Please help on this.
class Program
{
static void Main(string[] args)
{
string[] sub = subjects();
Console.WriteLine(sub);
}
public static string[] subjects()
{
Console.WriteLine("Please Enter How many Subject Do you Want to input");
int limit = System.Convert.ToInt32(Console.ReadLine());
string[] Subjects = new string[limit];
for (int i = 0; i < limit; i++)
{
Console.WriteLine("Please Enter Subject Name " + i);
Subjects[i] = Console.ReadLine();
}
return Subjects;
}
}
The reason that Program is printing system.string[] is because class string[] does not override ToString method, so the default one is used and the default ToString returns type name, not the "value" inside type.
You can use for example String.Join method to build one string from array of strings and put given delimiter between each string:
Console.WriteLine(String.Join(", ", sub));
Console.WriteLine(sub)
wont show you anything of value (it prints the sub type), since you are not referencing any value of the sub array, e.g sub[0]. Try this:
foreach (string s in sub)
{
Console.WriteLine(s);
}
Console.WriteLine(String.Join(", ", sub));
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a jagged array that contains other 1d string arrays:
string[] first = {"one","two"};
string[] second = {"three","four"};
string[][] jagged = {first,second};
When I try to get the sub-arrays, they give a null value (I might be doing something wrong):
foreach (string[] arr in jagged[][]) {
// My stuff here
}
Did I do something wrong in the array initialization progress or do I have to convert the sub-arrays somehow?
Just the foreach part is wrong.
I have tested it like as follows:
string[] first = { "one", "two" };
string[] second = {"three","four"};
string[][] jagged = {first,second};
foreach (string[] arr in jagged)
{
Console.WriteLine(string.Join(",", arr));
}
Output:
one, two
three, four
It should be:
foreach (string[] arr in jagged)
{
// My stuff here
}
I pasted your code on my local environment and could iterate just fine.
If you use jagged[][] in your loop, then possibly you get a type conversion failed message:
Cannot convert type 'char' to 'string'.
Instead use jagged in your loop.
public class Program
{
public static void Main()
{
string[] first = {"one","two"};
string[] second = {"three","four"};
string[][] jagged = {first,second};
foreach (string[] arr in jagged)
{
//Your code
}
}
}
This question already has answers here:
What is the .NET equivalent of PHP var_dump?
(5 answers)
Closed 6 years ago.
I need to dump the content of arrays or objects and I am interested to know if in C# we have something like PHP instruction var_dump.
The objective is to not build a loop to use every property or content of array or object and print with Console.WriteLine.
The closest thing would probably be string.Join:
Console.WriteLine(string.Join(", ", myEnumOfObjects));
It would not automatically include "every property or content of array or object" into the output, though - if you want that to happen, you need to override the ToString method of the object being printed:
class MyObject {
public string Name {get;set;}
public DateTime Dob {get;set;}
public override string ToString() {
return string.Format("{0} - {1}", Name, Dob);
}
}
I think there aren't direct equivalent of var_dump php function.
You must use reflection to write an equivalent function.
If you search in web, you can easily find code which do it.
For example : http://ruuddottech.blogspot.fr/2009/07/php-vardump-method-for-c.html
When you insert a break point you can easily view the contents of an array by hovering your mouse over it.
or any of these:
You are probably using Console.WriteLine for printing the array.
int[] array = new int[] { 1, 2, 3 };
foreach(var item in array)
{
Console.WriteLine(item.ToString());
}
If you don't want to have every item on a separate line use Console.Write:
int[] array = new int[] { 1, 2, 3 };
foreach(var item in array)
{
Console.Write(item.ToString());
}
or string.Join (in .NET Framework 4 or later):
int[] array = new int[] { 1, 2, 3 };
Console.WriteLine(string.Join(",", array));
from this question: How to print contents of array horizontally?
I know you want to avoid loop, but if its just for the sake of writing multiple lines of code, below is a one liner loop that could allow you to print data with single line for Objects extend ForEach Method
List<string> strings=new List<string>{"a","b","c"};//declare one
strings.ForEach(x => Console.WriteLine(x));//single line loop...for printing and is easier to write