Vivek
Vivek

Reputation: 103

How to print object of type List<string> in C#

I am new to c# and I want to print object which is of List type. How can I print this object and display list in console? Below is my code:

class Program{
    public List<double> GetPowersOf2(int input)
    {
        var returnList = new List<double>();
        for (int i = 0; i < input + 1; i++)
        {
            returnList.Add(Math.Pow(2, i));
        }
         returnList.ForEach(Console.WriteLine);//display list from method.
        return returnList;
    }
    static void Main(String[] args)
    {
        Program p = new Program();
        Console.WriteLine(p.GetPowersOf2(2));//not display list...
    }
}

It gives error: System.Collections.Generic.List`1[System.Double] Please suggest solution. Thanks in advance.

Upvotes: 1

Views: 3540

Answers (5)

Yair Halberstadt
Yair Halberstadt

Reputation: 6841

var powers=p.GetPowersOf2(2);
foreach(double power in powers) Console.Writeline(power+", ");

The reason why your original code did not work is is because the default implementation of .ToString() is to simply print the name of class. Only in certain classes is this overridden.

If you want to change the default behavior of .ToString for your list you will have to derive a class from List.

    public class PrintableList<T> : List<T>
    {
        public override string ToString()
        {
            string result = "";
            foreach (T obj in this)
            {
                result += obj.ToString() + "\n";
            }
            return result;
        }
    }

Use this in place of your list in your code, and your code will work fine.

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186708

Try a simple Linq and Join the outcome into a single string:

  // Let's use BigInteger instead of Double to represent 2's powers
  using System.Numerics;

  ...

  int input = 12;

  string report = string.Join(Environment.NewLine, Enumerable
    .Range(0, input)
    .Select(index => BigInteger.Pow(2, index)));

  Console.Write(report);

Outcome:

1
2
4
8
16
32
64
128
256
512
1024
2048

Upvotes: 3

TheSkimek
TheSkimek

Reputation: 342

Dont return the List and just call it like that:

class Program{
    public void GetPowersOf2(int input)
    {
        var returnList = new List<double>();
        for (int i = 0; i < input + 1; i++)
        {
            returnList.Add(Math.Pow(2, i));
        }
         returnList.ForEach(Console.WriteLine);//display list from method.
        //return returnList;
    }
    static void Main(String[] args)
    {
        Program p = new Program();
        p.GetPowersOf2(2);
    }
}

OR return it like that:

class Program{
    public List<double> GetPowersOf2(int input)
    {
        var returnList = new List<double>();
        for (int i = 0; i < input + 1; i++)
        {
            returnList.Add(Math.Pow(2, i));
        }
         //returnList.ForEach(Console.WriteLine);//display list from method.
        return returnList;
    }
    static void Main(String[] args)
    {
        Program p = new Program();
        p.GetPowersOf2(2).ForEach(Console.WriteLine);//not display list...
    }
}

Upvotes: 0

sujith karivelil
sujith karivelil

Reputation: 29026

So you are aware of displaying a list in the console, as you did within the method(returnList.ForEach(Console.WriteLine);). Now you returning the list object to the calling method and now you don't know how to display it, right? Why don't you try the same here, like this:

p.GetPowersOf2(2).ForEach(Console.WriteLine);

Because the variable returnList is returning from the method, so definitely the same will receive in the calling method.

Upvotes: 0

rmjoia
rmjoia

Reputation: 980

Using Linq:

public class Program{

public List<double> GetPowersOf2(int input)
{
    var returnList = new List<double>();

    for (int i = 0; i < input + 1; i++)
    {
        returnList.Add(Math.Pow(2, i));
    }

    return returnList;
 }

    public static void Main(String[] args)
    {
        Program p = new Program();
        p.GetPowersOf2(2).ForEach(Console.WriteLine);
    }
}

Upvotes: 0

Related Questions