用于数组填充的foreach [英] foreach for array filling

查看:81
本文介绍了用于数组填充的foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是C#的新手.
我想用foreach代替for.

编码为:

Hi,

I´m new to C#.
I want to use foreach instead of for.

Coding with for:

String[] properties = new String[] { "co", "extensionAttribute1", "mail" , "homeMDB"};
String[] result = new String[] {};

for (int i = 0; i < properties.Length; i++)
      {
         searcher.PropertiesToLoad.Add( properties[i] );
      }

SearchResult searchresult = searcher.FindOne();

for (int j = 0; j < properties.Length; j++)
            {
                if (searchresult.Properties.Contains(properties[j]))
                {
                    if (searchresult.Properties[properties[j]][0].ToString() != null)
                    {
                        result[j] = searchresult.Properties[properties[j]][0].ToString();
                    } 
            }




我尝试用foreach编写它,但首先还是为了成功,但是现在我不知道如何填充String []结果:




I tried to write it with foreach, first for sucessful, but now i don´t know how to fill the String[] result:

foreach (String s in properties)
            {
                searcher.PropertiesToLoad.Add(s);
            }

foreach (String s in properties)
            {
                if (searchresult.Properties.Contains(s))
                {
                    if (searchresult.Properties[s][0].ToString() != null)
                    {
                        result[????]= searchresult.Properties[s][0].ToString();
                    }
                }
            }


任何人都可以帮我吗?


Can anyone help me please?

推荐答案

如果您要使用foreach(这很有意义)并假设result 数组中有足够的空间,那么您只需要设置一个自己的整数即可:
If you want to use a foreach (and it makes some sense) and assuming there is enough space in the result array, then you just have to set up an integer of your own:
int j = 0;
foreach (String s in properties)
            {
                if (searchresult.Properties.Contains(s))
                {
                    if (searchresult.Properties[s][0].ToString() != null)
                    {
                        result[j++]= searchresult.Properties[s][0].ToString();
                    }
                }
            }

或者,我将result设置为字符串列表,并在每次添加到列表中:

Alternatively, I would set up result as a List of strings, and add to it each time:

foreach (String s in properties)
            {
                if (searchresult.Properties.Contains(s))
                {
                    if (searchresult.Properties[s][0].ToString() != null)
                    {
                        result.Add( searchresult.Properties[s][0].ToString());
                    }
                }
            }

如果之后需要数组,则List具有ToArray方法.这样,您就不会分配不需要的空间,不会在数组的末尾留下空条目,或者因为您没有分配足够的元素而用完数组.

If you need an array afterwards, then List has a ToArray method. This way, you are not assigning space you don''t need, or leaving null entries at the end of the array, or running out of array because you didn''t assign enough elements.


只需使用一个简单的计数器变量,该变量在foreach 循环运行时每次递增.
Just use a simple counter variable that is incremented every single time the foreach loop runs.


这篇关于用于数组填充的foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆