开发者

General Questions about List

Hi I have a general question about Lists in C#. Here is my Code:

public List<string> Example()
    {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSStorageDriver_FailurePredictStatus");

        List<string> output = new List<string>();

        foreach (ManagementObject queryObj in searcher.Get())
        {
            output.Add(System.Convert.ToString(queryObj["InstanceName"]));
        }

        return output;
    }

and now I want to give the first i开发者_开发问答nput out

public FormMain()
    {

        Debug.WriteLine(Example(1));


    }

No overload for method 'output' takes 1 arguments

I hope you can explain me this and sorry for my question, I am an absoltue beginner

Best wishes


Well output is a List. Since you have coded Example as a method, which returns a list, to access it you need to call it using method syntax with empty parentheses. The return value is an instance of a List<string>. If you hit the decimal point after typing Example() you will see in intellisense the members of this object. One of them will show as square brackets like this []. this is the member you need to use to access whatever you have put in the list. The values you would provide are zero-based, that is they start at zero (for the first item in the list), and increase from there. So to access the first item in the list, you would write:

Debug.WriteLine(Example()[1]);

using the square brackets, not parentheses. You still need the parentheses in Example(), because it is a method... If you recoded it as a property:

public List<string> Example    
{     
   get 
   {   
      ManagementObjectSearcher searcher = 
        new ManagementObjectSearcher("root\\WMI", 
          "SELECT * FROM MSStorageDriver_FailurePredictStatus");        
      List<string> output = new List<string>();        
      foreach (ManagementObject queryObj in searcher.Get())
          output.Add(System.Convert.ToString(queryObj["InstanceName"]));  
      return output;
   }            
}

Then you would not need those parentheses and could just write

Debug.WriteLine(Example[1]);


How about this?

using System.Linq;

public List<string> Example()
{
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSStorageDriver_FailurePredictStatus");

    return searcher.Get().ToList();
}

public void Test()
{
    var myList = Example();
    var element = myList[0];
}


try:

Debug.WriteLine(Example()[1]);

EDIT: if you are looking for the first element you should use 0 rather than 1 as others pointed out.


You should use:

Example()[0];

The method Example() returns a List<string>, you can visit its elements by index directly, something like mylist[0]. And the first element has the index 0 instead of 1 in C#.


Bolu's answer was good, except it should be a 0 instead of a 1 - c# List and arrays are zero-based.


I think the error you get is: No overload for method 'Example' takes 1 arguments not No overload for method 'output' takes 1 arguments.

It denotes that the method Example is not prepared to take 1 argument.

to be able to do that:

Debug.WriteLine(Example()[0]);

Since you said the first output, that's why the index is zero here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜