Monday, 29 October 2018

IndexOf keyword in .NET

IndexOf is an extension method (default functionality can be overridden) on string.
Method will return index number of a character position in string.

Take a look in this example

public static void Main()
 {
  var character = "ABS";
  Console.WriteLine(character.IndexOf("B"));
 }

We are expecting to see 1 in Console.

If character does not exist in the string method will return -1.


public static void Main()
 {
  var character = "ABS";
  Console.WriteLine(character.IndexOf("F"));
 }

Above method will return -1.