Q:

Write a C# Sharp program to compare a given string with set of strings

0

Write a C# Sharp program to compare a given string with set of strings. 

Expected Output :

Bad argument: TestClass (type TestClass)                                         
Comparing 'some text' with 'TestClass': -1                                       
Bad argument: 123 (type Int32)                                                   
Comparing 'some text' with '123': 1                                              
Comparing 'some text' with 'some text': 0                                        
Comparing 'some text' with 'Some Text': -1

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
public class TestClass
{}
public class Example32 
{
   public static void Main()
   {
      var test = new TestClass();
      Object[] objectsToCompare = { test, test.ToString(), 123,
                                    123.ToString(), "some text",
                                    "Some Text" };
      string s = "some text";
      foreach (var objectToCompare in objectsToCompare) {
         try {
            int i = s.CompareTo(objectToCompare);
            Console.WriteLine("Comparing '{0}' with '{1}': {2}",
                              s, objectToCompare, i);
         }
         catch (ArgumentException) {
            Console.WriteLine("Bad argument: {0} (type {1})",
                              objectToCompare,
                              objectToCompare.GetType().Name);
         }
      }
   }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now