在C# 开发中往往使用foreach 循环语句 来代替for循环语句。foreach 比 for 更加简洁高效。

 
        foreach :
                foreach (var item in arr)
                {
                        item .....
                }
 
        for :
                for(int i = 0; i
                {
                        arr[i].....
                }

        显然 for语句直接就存在索引变量,通过索引获取值。
        但在实际操作中,使用foreach 有时需要用到索引。
        要获取foreach的索引值,因为没有直接的索引值,人们最容易想到的解决方法是在foreach语句外面定义索引变量,然后在foreach语句内自加,以此获取索引。例如:
 
        int i = 0;
        foreach(var item in arr)
        {
                i++;
                item....
        }
 
        这样是实现了,但是简单地使用indexOf函数就可以获取到索引值,例如:
 
        foreach(var item in arr)
        {
                int index = arr.indexOf(item); //index 为索引值
                item....
        }
 
        此文,仅为留下开发成长的足迹,和提供给有需要的开发者。

C# foreach 中获取索引index的方法[转]的更多相关文章

  1. IndexOf、LastIndexOf、Substring的用法及C# foreach 中获取索引index的方法

     String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置 ...

  2. (转)C# foreach 中获取索引index的方法

    在C# 开发中往往使用foreach 循环语句 来代替for循环语句.foreach 比 for 更加简洁高效.           foreach :                 foreach ...

  3. C# foreach 中获取索引index的方法

    方法一: int i = 0; foreach (var item in arr) { i++; } 方法二: foreach (var item in arr) { int index = arr. ...

  4. foreach 中获取索引index的方法

    一样,很少用到,记下来先 主要代码: foreach (var item in arr) { int index = arr.indexOf(item); //index 为索引值 }

  5. android中获取root权限的方法以及原理(转)

    一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. 二. Root 的介绍 1. Root 的目的 可以让我们拥有 ...

  6. dede织梦栏目页和文章页中获取当前栏目名称方法

    一般情况下,在dede织梦系统中列表页.栏目页和文章页中获取当前所在栏目名称只需要代码:{dede:type}[field:typename]{/dede:type}即可,不需要定义ID,默认的就是当 ...

  7. Android中获取文件路径的方法总结及对照

    最近在写文件存贮,Android中获取文件路径的方法比较多,所以自己也很混乱.找了好几篇博客,发现了以下的路径归纳,记录一下,以备不时之需 Environment.getDataDirectory() ...

  8. LoadRunner中获取当前系统时间方法

    LoadRunner中获取当前系统时间方法 发表于:2017-6-02 11:41  作者:人生_0809   来源:51Testing软件测试网采编 字体:大 中 小 | 上一篇 | 下一篇 | 打 ...

  9. Java编程中获取键盘输入实现方法及注意事项

    Java编程中获取键盘输入实现方法及注意事项 1. 键盘输入一个数组 package com.wen201807.sort; import java.util.Scanner; public clas ...

随机推荐

  1. mysql报错Multi-statement transaction required more than 'max_binlog_cache_size' bytes of storage

    mysql报错Multi-statement transaction required more than 'max_binlog_cache_size' bytes of storage 在执行cr ...

  2. python实现根据当前时间创建目录并输出日志

    举个例子:比如我们要实现根据当前时间的年月日来新建目录来存放每天的日志,当前时间作为日志文件名称:代码如下: #!/usr/bin/env python3 # _*_ coding: utf-8 _* ...

  3. [django]django缓存

    发现搞了全局缓存后,刷新得不到最新数据了. 还好有过期时间 redis常用: https://www.cnblogs.com/fansik/p/5483060.html django-redis缓存: ...

  4. [LeetCode] 830. Positions of Large Groups_Easy tag: Two Pointers

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

  5. [Java] Create File with java.io.File class

    Create a file with some content in some specific location. The reference is here. /** * Write fileCo ...

  6. thrift 安装历程

     安装Boost 支持 C++ 编译安装除gcc和gcc-c++之外,还需要两个开发库:bzip2-devel 和python-devel,因此在安装前应该先保证这两个库已经安装:# yum inst ...

  7. nvm(Node Version Manager)管理node版本

    nvm全称Node Version Manager是 Nodejs 版本管理器,它让我们能方便的对 Nodejs 的版 本进行切换. nvm 的官方版本只支持 Linux 和 Mac. Windows ...

  8. GO language for windows

    我记得我们已经下载完成了windows 下Go 的安装包 go1.9.windows-amd64.msi下面接着说吧 GO 在Windows 上也是按照linux 惯例来编程的,所以,你还需要一个wi ...

  9. .Net Core资源

    官网:https://dotnet.github.io/ 1.开发环境 vs2015安装: .net core sdk : https://download.microsoft.com/downloa ...

  10. Amber中的一些option设置及名词

    详细请见AMBER官方文档第18章第6节(18.6) Amber16.pdf The settings can be summarized as follows: imin=1  Choose a m ...