Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将字符串值传递给集合?在C中#_C#_.net_List_Collections_Ranorex - Fatal编程技术网

C# 如何将字符串值传递给集合?在C中#

C# 如何将字符串值传递给集合?在C中#,c#,.net,list,collections,ranorex,C#,.net,List,Collections,Ranorex,这是我的代码: List<string> deviceScreenshot=new List<string>(); List<string> fiddlerScreenshot=new List<string>(); if(string.IsNullOrEmpty(summary[3].ToString())==false) deviceScreenshot=summary[3]; else deviceScreenshot="

这是我的代码:

List<string> deviceScreenshot=new List<string>(); 
List<string> fiddlerScreenshot=new List<string>();

if(string.IsNullOrEmpty(summary[3].ToString())==false)
    deviceScreenshot=summary[3];
else
    deviceScreenshot="Screenshot not found";

if(string.IsNullOrEmpty(summary[4].ToString())==false)
    fiddlerScreenshot=summary[4];
else
    fiddlerScreenshot="Screenshot not found";
List deviceScreenshot=new List();
List fiddlerScreenshot=新列表();
if(string.IsNullOrEmpty(summary[3].ToString())==false)
deviceScreenshot=汇总[3];
其他的
deviceScreenshot=“未找到屏幕截图”;
if(string.IsNullOrEmpty(摘要[4].ToString())==false)
fiddlerScreenshot=摘要[4];
其他的
fiddlerScreenshot=“未找到屏幕截图”;
我收到以下错误消息

无法将类型“string”隐式转换为 “System.Collections.Generic.List”(CS0029)- D:\automation\OmnitureStatistics\OmnitureStatistics\TeardownUserCode.cs:144,23

请告诉我这个问题的解决办法

您必须使用
列表的
Add()
方法,如下所示

  List<string> deviceScreenshot=new List<string>();
  List<string> fiddlerScreenshot=new List<string>();


  if(string.IsNullOrEmpty(summary[3].ToString())==false)
      deviceScreenshot.Add(summary[3]);
  else
      deviceScreenshot.Add("Screenshot not found");

  if(string.IsNullOrEmpty(summary[4].ToString())==false)
      fiddlerScreenshot.Add(summary[4]);
  else
      fiddlerScreenshot.Add("Screenshot not found");
List deviceScreenshot=new List();
List fiddlerScreenshot=新列表();
if(string.IsNullOrEmpty(summary[3].ToString())==false)
设备筛选说明添加(摘要[3]);
其他的
deviceScreenshot.Add(“未找到屏幕截图”);
if(string.IsNullOrEmpty(摘要[4].ToString())==false)
添加(摘要[4]);
其他的
添加(“未找到屏幕截图”);

必须使用List类的Add()方法。这就是向列表中添加项目的方式

if(string.IsNullOrEmpty(summary[3].ToString())==false)
    deviceScreenshot.Add(summary[3]);
else
    deviceScreenshot.Add("Screenshot not found");

if(string.IsNullOrEmpty(summary[4].ToString())==false)
    fiddlerScreenshot.Add(summary[4]);
else
    fiddlerScreenshot.Add("Screenshot not found");

您可以将逻辑移动到助手方法,并将静态字符串保存在资源文件中

如果summary是一个列表或数组If string,则不需要调用ToString()

类程序
{
静态void Main(字符串[]参数)
{    
新程序().AddStringToCollection();
}             
私有void AddStringToCollection()
{
var summary=新字符串[]{“A”、“B”、“C”、“D”};
var deviceScreenshot=新列表();
var fiddlerScreenshot=新列表();
附录除空格外(deviceScreenshot,摘要[3]);
附录除空格外(fiddlerScreenshot,摘要[4]);
}
//如果可能,移动到资源文件
const string NotFoundText=“未找到屏幕截图”;
//在实用程序类中,这也可以是一个扩展方法
私有void AppendExceptWhiteSpace(列表,字符串值)
{
//不确定是否需要空字符串,否则将更改回IsNullOrEmpty
string text=string.IsNullOrWhiteSpace(值)
?NotFoundText
:价值;
列表。添加(文本);
}    
}
我不确定什么是“摘要”,它没有被提及,所以我将其视为参数列表

  • 由于缺少“”,您的比较将始终执行相同的操作: 摘要[3]。ToString())==“false”//string-to-string

  • 添加到列表应类似于: deviceScreenshot.Add(摘要[3].ToString()); 我知道大多数答案,但不知怎的,他们忘记了这里的“ToString()”(除非它是一个字符串列表。在本例中,删除这两种情况下的“ToString()”


  • 什么是
    deviceScreenshot
    它是list-list-deviceScreenshot=new list();list fiddlerScreenshot=new list();如果
    deviceScreenshot
    是集合,那么您可以这样添加“deviceScreenshot.add(summary[3]);`与这里的问题无关,但您可以使您的if条件看起来像
    if(!string.IsNullOrEmpty(summary[3].ToString())
    而不是
    如果(string.IsNullOrEmpty(summary[3].ToString())==false)
    使它看起来更好一点。嗨@Rahul,它真的很有效..谢谢:)。。。“deviceScreenshot.add(摘要[3]);”之间的区别是什么和“deviceScreenshot=摘要[3];”@JonSkeet感谢更新此代码无法编译!deviceScreenshot.Add(deviceScreenshot=摘要[3]);
    class Program
    {
        static void Main(string[] args)
        {    
            new Program().AddStringToCollection();
        }             
    
        private void AddStringToCollection()
        {
            var summary = new string[] {"A", "B", "C", "", "D"};
    
            var deviceScreenshot = new List<string>();
            var fiddlerScreenshot = new List<string>();
    
            AppendExceptWhiteSpace(deviceScreenshot, summary[3]);
            AppendExceptWhiteSpace(fiddlerScreenshot, summary[4]);
        }
    
        //move to a resource file if possible
        const string NotFoundText = "Screenshot not found";
    
        //in a utility class this could also be an extension method
        private void AppendExceptWhiteSpace(List<string> list, string value)
        {
            //not sure if you want empty strings, otherwise change back to IsNullOrEmpty
            string text = string.IsNullOrWhiteSpace(value) 
                    ? NotFoundText 
                    : value;
    
            list.Add(text);
        }    
    }