4
Answers

NP80 compared with -1

Maha

Maha

16y
1.8k
1

Hi Guys

 

NP80 compared with -1

 

In this program why it should compared with -1. “if (-1 != s.IndexOf("COM"))”.

 

Please explain the reason.

 

Thank you

 

using System;

class Foreach

{

   // Digging into an array using foreach.

   public static int Main(string[] args)

   {

      string[] arrBookTitles = new string[] { "Complex Algorithms",

                                                "COM for the Fearful Programmer",

                                                   "Do you Remember Classic COM?",

                                                      "C# and the .NET Platform",

                                                         "COM for the Angry Engineer" } ;

 

         int COM = 0, NET = 0;

         // Assume we are not looking for books on COM interop.

 

      foreach (string s in arrBookTitles)

      {

         if (-1 != s.IndexOf("COM"))

               COM++;

         else if(-1 != s.IndexOf(".NET"))

               NET++;

      }

 

      Console.WriteLine("Found {0} COM references and {1} .NET references.",

            COM, NET);

 

      return 0;

   }

}

/*

Found 3 COM references and 1 .NET references.

*/

Answers (4)