[Solved] how to return bool in void C# [closed]


if you want to ‘return ‘ a bool from a function there are several ways

The obvious one is

bool CheckQuote(string item, string line)
{
    if (!line.Contains(item))
    {
         return false
    
    }
    return true;
}

used like this
if(!CheckQuote(“a”,”b”))
// error case

if for some reason the method has to be void (why tho), you can do

void CheckQuote(string item, string line, out bool result)
{
    if (!line.Contains(item))
    {
        result = false
    
    } else{
       result = false;
}

then call it like this

 bool check;
 CheckQuote("a", "b", out check);
 if(!check)
    // error case

one way more convoluted way

 public class Checker{
      bool Result {get;set;}
      void CheckQuote(string item, string line)
      {
          if (!line.Contains(item))
          {
               Result = false;
           } else{
               Result = true;
           }
      }
  }

used like this

  Checker check = new Checker();
  check.CheckQuote("a","b");
  if(!check.Result)
      // error case 

but thatsway over the top

solved how to return bool in void C# [closed]