对于c#.net中的每个循环 [英] For And For Each Loop In c#.net

查看:69
本文介绍了对于c#.net中的每个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#.net中的For和For循环之间的差异是什么?

What is Diffrance Between For And For Each loop in C#.net

推荐答案

用Google搜索?

For Vs Foreach - Benchmark 2013 [ ^ ]



如果你期待对于即将到来的采访的答案,那么这是不正确的学习方式。你应该从头开始学习C#。

这里有很多方法可以在互联网上学习任何东西。

需要教育 [ ^ ]
Googled?
For Vs Foreach - Benchmark 2013[^]

If you expect answers for your upcoming interviews then it's not right way to learn. You should start to learn C# from start.
Here more than couple of ways to learn anything in internet.
Education Needed[^]


简单:foreach循环对集合中的每个对象起作用,并在循环中将该对象作为适当的类型提供:

Simple: A foreach loop works on each object in a collection, and provides that object as the appropriate type within the loop:
List<string> list = new List<string>() {"hello", "world"};
foreach (string s in list)
   {
   Console.WriteLine(s);
   }
// s is not available here.

当你必须迭代集合中的每个元素但是不关心它们是什么索引时,这非常有用。相比之下,for循环更强大,但更难阅读并需要更多关注。使用for循环执行相同的操作将是:

This is really useful when you have to iterate though each element in a collection, but don't care what index they are at. In contrast, a for loop is more powerful, but harder to read and needs more care. To do the same thing with a for loop would be:

List<string> list = new List<string>() { "hello", "world" };
for (int index = 0; index < list.Count; index++)
    {
    string s = list[index];                
    Console.WriteLine(s);
    }

一般来说,如果你可以使用foreach,你应该 - 但你还必须记住你不能在foreach中以任何方式改变集合。如果您尝试,您将收到运行时错误。您可以在for循环中更改集合(例如添加或删除项目)而不会出现任何问题。



整理缩进 - OriginalGriff [/ edit]

Generally, if you can use a foreach, you should - but you also have to remember that you cannot alter the collection in any way inside a foreach. If you try, you will get a runtime error. You can alter the collection (adding or removing items for example) within the for loop without any problems.

[edit]Tidied up indentation - OriginalGriff[/edit]


for循环:for循环执行语句或语句块,直到条件为false,其中包含语句重复的长度。 for循环有三个表达式。



for循环的语法:for(值初始化;条件;递增/递减)



ex:for(int i = 0; i< = 5; i ++)

{

Console.WriteLine(i);

$







Fooreach循环:在foreeach循环中不需要指定循环绑定值像最小值或最大值一样。为数组或对象组的每个元素执行一个语句。



fooreach循环系统:foreach(对象/数组名称组中的变量声明)



ex:

int a = 0;



int [] no = new int [] {0,1,2,3,4};



foreach(int i in no)



{



a = a + i;



}
for Loop: for loop execute the statement or block of statement until the condition is false where it's containing with the length for statement repetition. for loop have three expression.

syntax of for loop: for(Value initialization;condition;increment/decrements)

ex: for(int i=0;i<=5;i++)
{
Console.WriteLine(i);
}



Fooreach loop: In foreeach loop do not need to specify the loop bound value as like minimum or maximum value. Execute a statement for each element of array or group of object.

systex of fooreach loop: foreach(variable declaration in group of object/array name)

ex:
int a = 0;

int[] no = new int[] { 0, 1, 2, 3, 4 };

foreach (int i in no )

{

a = a + i ;

}


这篇关于对于c#.net中的每个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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