📜  C#8.0中的范围和指数

📅  最后修改于: 2021-05-29 15:45:23             🧑  作者: Mango

正如我们已经知道的范围和指数。我们在程序中多次使用它们,它们提供了一种简短的语法来表示或访问给定序列或集合中的单个或一系列元素。在本文中,我们将学习C#8.0中的范围和索引中新增的内容。在C#8.0中,在范围和索引中添加了以下新内容:

1.两种新类型

  • System.Range :它表示给定序列或集合的子范围。
  • System.Index :它表示给定序列或集合的索引。

2.两名新的运营商

  • ^运算符:最终运算符称为索引。它返回相对于序列或集合结尾的索引。与早期方法相比,这是找到末端元素的最紧凑,最简单的方法。
    // Old Method
    var lastval = myarr[myarr.Length-1]
    
    // New Method
    var lastval = myarr[^1]
    

    例子:

    // C# program to illustrate the use 
    // of the index from end operator(^)
    using System;
      
    namespace example {
      
    class GFG {
      
        // Main Method
        static void Main(string[] args)
        {
            // Creating and initializing an array
            int[] myarr = new int[] {34, 56, 77, 88, 90, 45};
      
            // Simple getting the index value
            Console.WriteLine("Values of the specified indexes:");
            Console.WriteLine(myarr[0]);
            Console.WriteLine(myarr[1]);
            Console.WriteLine(myarr[2]);
            Console.WriteLine(myarr[3]);
            Console.WriteLine(myarr[4]);
            Console.WriteLine(myarr[5]);
      
            // Now we use index from end(^) 
            // operator with the given index
            // This will return the end value
            // which is related to the specified
            // index
            Console.WriteLine("The end values of the specified indexes:");
            Console.WriteLine(myarr[^1]);
            Console.WriteLine(myarr[^2]);
            Console.WriteLine(myarr[^3]);
            Console.WriteLine(myarr[^4]);
            Console.WriteLine(myarr[^5]);
            Console.WriteLine(myarr[^6]);
        }
    }
    }
    

    输出:

    Values of the specified indexes:
    34
    56
    77
    88
    90
    45
    The end values of the specified indexes:
    45
    90
    88
    77
    56
    34
    

    说明:在上面的示例中,我们有一个名为myarr的int类型的数组。在这里,首先我们仅获取指定索引的值,即:

    34, 56, 77, 88, 90, 45
    Index : Value
     [0]  : 34
     [1]  : 56
     [2]  : 77
     [3]  : 88
     [4]  : 90
     [5]  : 45
    
    

    现在,借助^运算符可以找到指定索引的最后一个值:

    Index : Value
     [^1]  : 45
     [^2]  : 90
     [^3]  : 88
     [^4]  : 77
     [^5]  : 56
     [^6]  : 34
    

    重要事项:

    • 该运算符的工作类似于myarr [arr.Length]。
    • 如果使用它,则不允许使用myarr [^ 0],这将引发错误,因为结束索引从^ 1开始,而不是从^ 0开始,如以下示例所示:

      例子:

      // C# program to illustrate the use 
      // of the index from end operator(^)
      using System;
        
      namespace example {
        
      class GFG {
        
          // Main Method
          static void Main(string[] args)
          {
              // Creating and initializing an array
              int[] myarr = new int[] {34, 56,
                              77, 88, 90, 45};
        
              // Simply getting the index value
              Console.WriteLine("Values of the specified index:");
              Console.WriteLine(myarr[0]);
        
              // Now we use index from end(^)
              // operator with the given index
              // This will return the end value
              // which is related to the specified
              // index
              Console.WriteLine("The end values of the specified index:");
              Console.WriteLine(myarr[^0]);
          }
      }
      }
      

      输出:

    • 允许将索引用作变量,并且此变量放在方括号[]之间。如下例所示:

      例子:

      // C# program to illustrate how
      // to declare a index as a variable
      using System;
         
      namespace example {
         
      class GFG {
         
          // Main Method
          static void Main(string[] args)
          {
         
              // Creating and initializing an array
              int[] number = new int[] {1, 2, 3, 4,
                                    5, 6, 7, 8, 9};
         
              // Declare an index
              // as a variable
              Index i = ^6;
              var val = number[i];
         
              // Displaying number
              Console.WriteLine("Number: " + val);
          }
      }
      }
      

      输出:

      Number: 4
  • ..运算符:称为范围运算符。并将开始和结束指定为其给定范围的操作数。与较早的方法相比,这是从指定的序列或集合中查找元素范围的最紧凑,最简单的方法。
    // Old Method
    var arr = myemp.GetRange(1, 5);
    
    // New Method
    var arr = myemp[2..3]
    

    例子:

    // C# program to illustrate the 
    // use of the range operator(..)
    using System;
       
    namespace example {
       
    class GFG {
       
        // Main Method
        static void Main(string[] args)
        {
            // Creating and initializing an array
            string[] myemp = new string[] {"Anu", "Priya", "Rohit", 
                        "Amit", "Shreya", "Rinu", "Sumit", "Zoya"};
       
            Console.Write("Name of the employees in project A: ");
            var P_A = myemp[0..3];
            foreach(var emp1 in P_A)
                Console.Write($" [{emp1}]");
       
            Console.Write("\nName of the employees in project B: ");
            var P_B = myemp[3..5];
            foreach(var emp2 in P_B)
                Console.Write($" [{emp2}]");
       
            Console.Write("\nName of the employees in project C: ");
                var P_C = myemp[1..^2];
                foreach (var emp3 in P_C)
                    Console.Write($" [{emp3}]");
       
            Console.Write("\nName of the employees in project D: ");
            var P_D = myemp[..];
            foreach(var emp4 in P_D)
                Console.Write($" [{emp4}]");
       
            Console.Write("\nName of the employees in project E: ");
            var P_E = myemp[..2];
            foreach(var emp5 in P_E)
                Console.Write($" [{emp5}]");
       
            Console.Write("\nName of the employees in project F: ");
            var P_F = myemp[6..];
            foreach(var emp6 in P_F)
                Console.Write($" [{emp6}]");
       
            Console.Write("\nName of the employees in project G: ");
            var P_G = myemp[^3.. ^ 1];
            foreach(var emp7 in P_G)
                Console.Write($" [{emp7}]");
        }
    }
    }
    

    输出:

    Name of the employees in project A:  [Anu] [Priya] [Rohit]
    Name of the employees in project B:  [Amit] [Shreya]
    Name of the employees in project C:  [Priya] [Rohit] [Amit] [Shreya] [Rinu]
    Name of the employees in project D:  [Anu] [Priya] [Rohit] [Amit] [Shreya] [Rinu] [Sumit] [Zoya]
    Name of the employees in project E:  [Anu] [Priya]
    Name of the employees in project F:  [Sumit] [Zoya]
    Name of the employees in project G:  [Rinu] [Sumit]
    

    重要事项:

    • 使用range运算符创建范围时,它将不会添加最后一个元素。例如,我们有一个数组{1、2、3、4、5、6},现在我们要打印range [1..3],那么它将打印2、3。它不打印2、3, 4,
    • 在范围中,如果范围包含起点和终点索引(例如Range [start,end]),则这种类型的范围称为有界范围。
    • 在范围中,如果范围仅包含开始,结束索引,或者不包含范围和索引,例如Range [start ..],Range [.. end]或Range [..],则这些类型的范围为被称为无界范围。
    • 您可以将range用作变量,并将此变量放在方括号[]之间。如下例所示:

      例子:

      // C# program to illustrate how to
      // declare a range as a variable
      using System;
         
      namespace example {
         
      class GFG {
         
          // Main Method
          static void Main(string[] args)
          {
         
              // Creating and initializing an array
              int[] number = new int[] {1, 2, 3, 4,
                                    5, 6, 7, 8, 9};
         
              Console.Write("Number: ");
         
              // Declaring a range as a variable
              Range num = 1..3;
              int[] val = number[num];
         
              // Displaying number
              foreach(var n in val)
                  Console.Write($" [{n}]");
          }
      }
      }
      

      输出:

      Number:  [2] [3]