System.Collections.Generic.List`1[System.Int32]

titotitus

Member
Joined
Feb 12, 2020
Messages
14
Programming Experience
Beginner
C#:
using System;
using System.Collections.Generic;
using System.Linq;

namespace exercise_75
{
  class Program
  {

   public static void PrintNumbersInRange(List<int> numbers, int lowerLimit, int upperLimit)
   {
  
    foreach (int number in numbers.Where (number => number >= lowerLimit && number <= upperLimit));
    Console.WriteLine(numbers);
 
   }
      
    public static void Main(string[] args)
   {
     // Example method calls for testing your method.
  List<int> numbers = new List<int>();
  numbers.Add(3);
  numbers.Add(2);
  numbers.Add(6);
  numbers.Add(-1);
  numbers.Add(5);
  numbers.Add(1);
 
 
    Console.WriteLine("The numbers in the range [0, 5]");
    PrintNumbersInRange(numbers, 0, 5);     

    }

    }
  }


this code gives me this error!!! how do i fix it
System.Collections.Generic.List`1[System.Int32]
 
for each number in numbers
write number
 
That's because you're trying to change it in the wrong place. You need to change on the Console.WriteLine line. You want to write the current number on that line, not the collection containing the numbers. When you pass something to that method, it calls ToString on it, the result of which is the name of the type for most types. That's exactly what you're seeing, i.e. the full name of the type of that numbers variable.
 
foreach (int number in numbers.Where (number => number >= lowerLimit && number <= upperLimit));
Console.WriteLine(number)

The name 'number' does not exist in the current context.....error when i run it
 
You have a spurious semicolon at the end of the foreach line. The code you have is equivalent to
this:
C#:
foreach (int number in numbers.Where (number => number >= lowerLimit && number <= upperLimit))
{
}
Console.WriteLine(number);
when what you obviously want is this:
C#:
foreach (int number in numbers.Where (number => number >= lowerLimit && number <= upperLimit))
{
    Console.WriteLine(number);
}
 
i put the semi colons, its a no go...i get

"Possible mistaken empty statement" on the for each....

and
number does not exist in the current context
 
If it didn't work then you did it wrong. If we can't see what you did then we can't see what you did wrong. Post the code you have and be sure to format it correctly. We can then determine what's wrong with it. That said, if you wrote the code as I did then it would be working, as it was for me.
 
C#:
using System;
using System.Collections.Generic;
using System.Linq;

namespace exercise_75
{
  class Program
  {

   public static void PrintNumbersInRange(List<int> numbers, int lowerLimit, int upperLimit)
  
   {
    foreach (int number in numbers.Where (number => number >= lowerLimit && number <= upperLimit));
    {
    Console.WriteLine(number);
    }
   }
      
    public static void Main(string[] args)
   {
     // Example method calls for testing your method.
  List<int> numbers = new List<int>();
  numbers.Add(3);
  numbers.Add(2);
  numbers.Add(6);
  numbers.Add(-1);
  numbers.Add(5);
  numbers.Add(1);
 
 
    Console.WriteLine("The numbers in the range [0, 5]");
    PrintNumbersInRange(numbers, 0, 5);     

    }

    }
  }
 
No, your problem is EXACTLY where I told you it was. I told you what the problem was and I even showed you what the code should be and you still got it wrong. There's no reason for me to repeat what I already posted. Go back and read post #6 PROPERLY and do what I instructed.
 
Back
Top Bottom