📜  如何在C#中获取索引值?

📅  最后修改于: 2021-05-29 16:22:21             🧑  作者: Mango

索引结构在C#8.0中引入。它表示可用于索引集合或序列的类型,并且可以从头开始或从头开始。您可以借助Index结构提供的Value属性来获取索引值。

句法:

public int Value { int get(); };

范例1:

// C# program to illustrate the 
// concept of the value property
using System;
  
namespace example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // Creating new indexes
        // Using Index() constructor
        var val1 = new Index(1, true);
        var val2 = new Index(2, true);
        var val3 = new Index(1, false);
        var val4 = new Index(3, false);
  
        // Getting the value of the index
        var res1 = val1.Value;
        var res2 = val2.Value;
        var res3 = val3.Value;
        var res4 = val4.Value;
  
        // Display index value
        Console.WriteLine("Index value is : " + res1);
        Console.WriteLine("Index value is : " + res2);
        Console.WriteLine("Index value is : " + res3);
        Console.WriteLine("Index value is : " + res4);
    }
}
}

输出:

Index value is : 1
Index value is : 2
Index value is : 1
Index value is : 3

范例2:

// C# program to illustrate the 
// concept of the value property
using System;
  
namespace example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // Creating and initializing an array
        string[] greetings = new string[] {"Hello", "Hola", "Namaste", 
                               "Bonjour", "Ohayo", "Ahnyounghaseyo"};
  
        // Creating index
        // Using Index() constructor
        var val1 = new Index(1, true);
        var val2 = new Index(2, false);
  
        // Checking the given both the 
        // index values are equal or not
        if (val1.Value.Equals(val2.Value) == true) 
        {
            Console.WriteLine("Both the indexes are equal and"+
              " their elements are : {0}, {1}", greetings[val1],
                                              greetings[val2]);
        }
  
        else {
            Console.WriteLine("Both the indexes are not equal"+
           " and their elements are : {0}, {1}", greetings[val1],
                                               greetings[val2]);
        }
    }
}
}

输出:

Both the indexes are not equal and their elements are : Ahnyounghaseyo, Namaste