Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Index Constructor in C#

  • Last Updated : 28 Nov, 2019

Index(Int32, Boolean) constructor of Index Struct in C# 8.0 is used to initialize a new index with the specified index position and a value which shows whether the index starts from the start or the end of the collection or sequence. If the specified index created from the end, then the index value of 1 will point to the last element and the index value of 0 will point beyond the last element.

Syntax:

public Index (int Value, bool FromEnd = false);

Here, Value is of index value and it is of System.Int32 type. The value of the index should be equal to or greater than zero. FromEnd is the boolean value which indicates that if the index starts from the start or from the end of the collection or sequence. Below codes executed on C# 8.0 or above versions:

Example 1:




// C# program to illustrate
// the use of Index constructor
using System;
  
namespace example {
  
class GFG {
  
    static void Main(string[] args)
    {
        // Creating and initializing an array
        string[] greetings = new string[] {"Hello", "Hola", "Namaste"
                                "Bonjour", "Ohayo", "Ahnyounghaseyo"};
  
        // Creating new indexes
        // Using Index() constructor
        var val1 = new Index(1, true);
        var val2 = new Index(2, true);
        var val3 = new Index(3, true);
        var val4 = new Index(1, false);
        var val5 = new Index(2, false);
        var val6 = new Index(3, false);
  
        // Getting the values of 
        // the specified indexes
        var res1 = greetings[val1];
        var res2 = greetings[val2];
        var res3 = greetings[val3];
        var res4 = greetings[val4];
        var res5 = greetings[val5];
        var res6 = greetings[val6];
  
        // Display indexes and their values
        Console.WriteLine("Index:{0} Value: {1}", val1, res1);
        Console.WriteLine("Index:{0} Value: {1}", val2, res2);
        Console.WriteLine("Index:{0} Value: {1}", val3, res3);
        Console.WriteLine("Index:{0} Value: {1}", val4, res4);
        Console.WriteLine("Index:{0} Value: {1}", val5, res5);
        Console.WriteLine("Index:{0} Value: {1}", val6, res6);
    }
}
}

Output:

Index:^1 Value: Ahnyounghaseyo
Index:^2 Value: Ohayo
Index:^3 Value: Bonjour
Index:1 Value: Hola
Index:2 Value: Namaste
Index:3 Value: Bonjour

Example 2:




// C# program to illustrate 
// the use of Index constructor
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(2, false);
  
        // Checking if the specified 
        // index start from end or not
        var res1 = val1.IsFromEnd;
        var res2 = val2.IsFromEnd;
        var res3 = val3.IsFromEnd;
        var res4 = val4.IsFromEnd;
  
        // Display indexes and their values
        Console.WriteLine("Index:{0} Start from end?: {1}", val1, res1);
        Console.WriteLine("Index:{0} Start from end?: {1}", val2, res2);
        Console.WriteLine("Index:{0} Start from end?: {1}", val3, res3);
        Console.WriteLine("Index:{0} Start from end?: {1}", val4, res4);
    }
}
}

Output:

Index:^1 Start from end?: True
Index:^2 Start from end?: True
Index:1 Start from end?: False
Index:2 Start from end?: False



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!