• 4-索引中的那些操作


    哈喽,放起假的第一天。

    接着之前的文档来观看,看到了索引。

    索引这个东西我们都很熟悉,就不太详细介绍了。

    但是今天看文档的时候发现还有一些新操作,先上代码看一眼。

     1             string[] words = new string[] 
     2             {
     3                     // index from start index from end 
     4             "The",  // 0 ^9 "quick", // 1 ^8 
     5             "brown", // 2 ^7 "fox", // 3 ^6 
     6             "jumped", // 4 ^5 
     7             "over", // 5 ^4 
     8             "the", // 6 ^3 
     9             "lazy", // 7 ^2 
    10             "dog" // 8 ^1 
    11             }; // 9 (or words.Length) ^0
    12 
    13             Console.WriteLine($"The last word is {words[^0]}");

    前面的都是很基础的操作,但是第13行在通过索引取值的时候,发现了  【^0】 这么个玩意儿,

    之前取值一般都是通过下标获取,或者在循环中依次迭代去获取。 但,words【^0】 这是个什么玩意儿?

    这下就明白了,但不幸的是,在我的vs15中这种写法跑不起来。

    没办法去跑出来看一下了。

    有兴趣有vs19的童鞋可以去试试看贴出来。

    还有这种写法

    string[] quickBrownFox = words[1..4]

    这种写法

    string[] lazyDog = words[^2..^0];

    当然我们主要使用的还是变量方式取值。

    int x = 12;
    int y = 25;
    
    Console.WriteLine($"{numbers[x..y].Length} is the same as {y - x}"); 
    Console.WriteLine("numbers[x..y] and numbers[y..z] are consecutive and disjoint:");

    好了,索引就说到这里,除了新版本带来的惊喜外,其他也没什么好说的。

  • 相关阅读:
    Google Analytics:为链接点击设定事件追踪的方法
    HTTP状态码大全
    jquery插件的编写
    2016.2.27日(开学)学习总结
    关于单文件上传的封装
    单文件的文件上传详细解释
    php中对象的串行化
    PDO的事物处理机制
    MySQL操作类的封装(PHP)
    smarty的简单介绍
  • 原文地址:https://www.cnblogs.com/SevenWang/p/13784891.html
Copyright © 2020-2023  润新知