Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, How do I call a function that is returning a list?

In C#, How do I call a function that is returning a list?

    static void Main(string[] args)  
    {  
        List<string> range = new List<string>();  
        range.ForEach(item => item.WildcardFiles());  //this is not working             
    }  

    List<string> WildcardFiles(string first)  
    {  
        List<string> listRange = new List<string>();  
        listRange.Add("q");  
        listRange.Add("s");  

        return listRange;  
    }  
like image 561
C N Avatar asked Nov 04 '11 22:11

C N


People also ask

What does << mean in C?

<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .

What does %d do in C?

%d is a format specifier, used in C Language. Now a format specifier is indicated by a % (percentage symbol) before the letter describing it. In simple words, a format specifier tells us the type of data to store and print. Now, %d represents the signed decimal integer.


2 Answers

There are various things wrong with your code:

  • You're creating an empty list, and then calling ForEach on it. That's not going to do anything.
  • You're trying to call WildcardFiles on a string, when it's not a method of string.
  • You're trying to call WildcardFiles which is an instance method in whatever your declaring type is, but without any instances of that type.
  • You're trying to call WildcardFiles without passing in an argument for the first parameter
  • You're ignoring the return value of the call to WildcardFiles
  • WildcardFiles ignores its parameter

Now I suspect you really wanted something like:

static void Main(string[] args)  
{  
    List<string> range = WildcardFiles();
    foreach (string item in range)
    {
        // Do something with item
    }
}  

static List<string> WildcardFiles()
{  
    List<string> listRange = new List<string>();  
    listRange.Add("q");  
    listRange.Add("s");  

    return listRange;  
}  
like image 101
Jon Skeet Avatar answered Sep 19 '22 02:09

Jon Skeet


I don't know what exactly you want but currently you should do:

range.ForEach(item => WildcardFiles(item));

and make your method static to work.

like image 34
Saeed Amiri Avatar answered Sep 19 '22 02:09

Saeed Amiri