Dedar
Dedar

Reputation: 141

operators in C#

I have an array of words and I want to remove any word that matched the pattern and also remove each word less than 3 characters of length. Is the following code able to do what i had in mind? thanks in advance.

           foreach (Match mtch in mc )

           {
               string p = @"[A-Z,a-z,0-9]" ;

               if (Regex.IsMatch(mtch.Value, p)  || mtch.Length < 3 )
                {
                   continue;
                }
              else
              {
               filter.Add(mtch.Value);
              }
           }

because I am really confused with using || operator.

Upvotes: 0

Views: 182

Answers (3)

Habib
Habib

Reputation: 223422

|| (OR Operator) works in the following manner. If condition 1 returns true and condition 2 returns false, the overall result is true.

true || true = true

true || false = true

false || true = true

flase || false = false

In your case:

  if (Regex.IsMatch(mtch.Value, p)  || mtch.Length < 3 )

it is checking if the value of mtch is matching the RegEx or the lenght of mtch is less then 3 then continue the loop execution. Now here you have two conditions, this will only go to the else part of if statement if the result of both conditions is false. i.e. value of mtch is not matching the Regex p and value mtch length is greater than or equal to 3

Upvotes: 3

Wouter de Kort
Wouter de Kort

Reputation: 39898

The || operator performs a logical-or.

It will first evaluate the left operand, if that's true then the expression will be true. If the left operand is false, the second operand will be evaluated.

If you run the following example as a console application you will see what happens:

using System;

namespace OrOperand
{
    class Program
    {
        static void Main(string[] args)
        {
            DisplayResult(true, true);
            DisplayResult(true, false);
            DisplayResult(false, true);
            DisplayResult(false, false);
        }

        private static void DisplayResult(bool left, bool right)
        {
            _left = left;
            _right = right;

            Console.WriteLine("Running " + left + " || " + right);

            if (Left() || Right())
            {
                Console.WriteLine(_left + " || " + _right + " is true");
            }
            else
            {
                Console.WriteLine(_left + " || " + _right + " is false");
            }
        }

        static bool _left = true;
        static bool _right = false;

        static bool Left()
        {
            Console.WriteLine("Left called");
            return _left;
        }

        static bool Right()
        {
            Console.WriteLine("Right called");
            return _right;
        }

    }
}

This will output:

Running True || True
Left called
True || True is true
Running True || False
Left called
True || False is true
Running False || True
Left called
Right called
False || True is true
Running False || False
Left called
Right called
False || False is false

Upvotes: 0

Bolu
Bolu

Reputation: 8786

The conditional-OR operator (||) performs a logical-OR of its bool operands. If the first operand evaluates to true, the second operand isn't evaluated. If the first operand evaluates to false, the second operator determines whether the OR expression as a whole evaluates to true or false.

|| Operator

Upvotes: 1

Related Questions