C#循环while

来源:百度知道 编辑:UC知道 时间:2024/05/18 15:51:58
for (int i = 0; i < ds.Tables["userreg"].Rows.Count; i++)

帮我用while写个这个循环,效果要一样。
Foreach循环有什么好处?

先说Foreach和For的区别,
Foreach是针对对象进行遍历的,不需要定义循环次数,但是有个缺点,Foreach遍历取的是只读数据,不能在Foreach中进行对象的增删改,而For循环就可以。

你这个改成while循环的代码如下:
int i=0;
while(i<ds.Table["userreg"].Rows.Count)
{
i++;
}

int i=0;
while(i<ds.Table["userreg"].Rows.Count)
{
i++;
}
这样就可以实现效果,你试试吧。
不知道你这是什么用意,用Foreach是一种不错的选择

int 1=0;
while(i<ds.Tables["userreg"].Rows.Count)
{
//循环体
i++;
}

foreach语句很简洁,但是它的优点不仅仅在于此,它的效率也是最高的。

foreach好处在于遍历元素时更加快捷方便

int i=0;
while(i<ds.Tables["userreg"].Rows.Count)
{

i++;
}