-2

I want to use the Contains() in a switch statement in C# but I don't know how. If you don't understand me, I'll show you the code so maybe you'll understand I guess.

Code I want to work:

public void Read(string text)
{
    switch (text.ToLower().Contains())
    {
        case "read":
            MessageBox.Show(text);
            break;
        case "not":
            MessageBox.Show(text);
            break;
    }
}

The upper code is the one I've attempted, but it doesn't work. So how can I use a function in a switch statement. I could use else ifs but I want to use it in a switch statement.

Axyclez
  • 15
  • 1
  • 9
  • 2
    Use `if` ... `else if` – Klaus Gütter Mar 09 '21 at 17:22
  • 1
    @Axyclez You seem dissatisfied with the suggestions. Perhaps you could instead elaborate why you want to use a `switch` statement instead of `if {} else if {}`? – Xerillio Mar 09 '21 at 17:26
  • 3
    @Axyclez Any differences in performance between `switch` and `if` are negligible and not worth considering. –  Mar 09 '21 at 17:33
  • 1
    @Axyclez I don't think people say your code is messy because you don't use a switch statement. You might want them to elaborate on that so you can focus on what matters to write better code. – insane_developer Mar 09 '21 at 17:46
  • Does this answer your question? [Use string.Contains() with switch()](https://stackoverflow.com/questions/7175580/use-string-contains-with-switch) – sous2817 Mar 09 '21 at 17:58
  • @sous2817 it doesn't – Axyclez Mar 10 '21 at 17:38

4 Answers4

1

Though if there are just 2cases then you shoul always goes with if , but still if you wana do with switch , you can do with pattern matching. you may need C# 7.0 for this I think

       public static void Read(string text)
    {
        switch (text)
        {
            case var read when text.ToLower().Contains("read"):
                MessageBox.Show(text);
                break;
            case var nott when text.ToLower().Contains("not"):
                MessageBox.Show(text);
                break;
        }
    }

Am not saying my solution is perfect. Am saying yes its possible with switch

yo chauhan
  • 12,079
  • 4
  • 39
  • 58
1
public void Read(string text)
{
  switch (text)
  {
    case string a when test.ToLower().Contains("read"):
      MessageBox.Show(text);
      break;
    case string b when test.ToLower().Contains("not"):
      MessageBox.Show(text);
      break;
  }
}

Also, the answer is here on how to use string.Contains() method with switch statement. Use string.Contains() with switch()

Daniel Kelsch
  • 393
  • 2
  • 10
0

Contains() function can not be used like this inside switch statement because it will either return true or false while switch statement applies to variables returning random cases like 1,2,3 , for your problem it will be best to use 'if else' instead.

Badar Khalil
  • 126
  • 9
0

As I can see, both cases are equal: either text contains "read" or "not" we should do the same: show the "text"

I suggest get rid of switch and have a single if:

using System.Linq; // to query with any

...

public void Read(string text) {
  // public method arguments validation
  if (null == text)
    throw new ArgumentNullException(nameof(text));

  string[] words = new string[] {"read", "not"};

  // ToLower() is a bad practice: 
  // - it may occure that we don't have to process the entire text
  // - StringComparison.OrdinalIgnoreCase is more readable
  // - words doesn't have to lowercase words only   
  if (words.Any(word => text.Contains(word, StringComparison.OrdinalIgnoreCase)))
    MessageBox.Show(text);
} 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215