CSharp - String characters Indexer Accessing

Introduction

A string's indexer returns a single character at the given index.

The indexing is is zero-indexed:

Demo

using System;
class MainClass{/* w  w w. ja v a2 s . c o m*/
   public static void Main(string[] args){
         string str  = "abcde";
         char letter = str[1];        // letter == 'b'
   }
}

string implements IEnumerable<char>, so you can foreach over its characters:

Demo

using System;
class MainClass{//from  www  . jav  a  2  s.  c om
   public static void Main(string[] args){
     foreach (char c in "123")
        Console.Write (c + ",");    // 1,2,3,
   }
}

Result

Exercise