How to return an array of strings in C#

The given needs are the following:

    Console.WriteLine(whoLikedMe(new string[] {})); // must be "no one likes this"
    Console.WriteLine(whoLikedMe(new string[] { "John" })); // must be "John likes this"
    Console.WriteLine(whoLikedMe(new string[] { "John", "Alicia" })); // must be "John and Alicia like this"
    Console.WriteLine(whoLikedMe(new string[] { "John", "Alicia", "Mark" })); // must be "John, Alicia and Mark like this"
    Console.WriteLine(whoLikedMe(new string[] { "John", "Alicia", "Mark", "Alex" })); // must be "John, Alicia and 2 others like this"

This is what i wrote so far in whoLikedMemethod:

public static string whoLikedMe(string[] names) {
    // .. Your Code goes HERE .. //
    for (int i = 0; i < names.Length; i ++)
    {
      if (names[i] == null)
      {
        string str = "no one likes this";
      }
      else if (names[i].Length == 1)
      {
        string str2 = string.Format("{0} likes this", names[i]);
      }
      else if (names[i].Length == 2)
      {
        string str3 = string.Format("{0} and {1} like this", names[i], names[i]);
      }
      else if (names[i].Length == 3)
      {
        string str4 = string.Format("{0}, {1} and {2} like this", names[i], names[i], names[i]);
      }
      else
      {
        string str5 = string.Format("{0}, {1} and 2 others like this", names[i], names[i]);
      }
      
    }
    //return names;  
  }


Read more here: https://stackoverflow.com/questions/66343141/how-to-return-an-array-of-strings-in-c-sharp

Content Attribution

This content was originally published by Christos at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: