Sunday, March 1, 2015

The carat operator in C# is going to resolve to true if one and only one condition is true!

For example, this test passes:

using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Whatever.Tests
{
   [TestClass]
   public class UnitTests
   {
      [TestMethod]
      public void Test()
      {
         bool foo = true;
         bool bar = true;
         bool baz = false;
         bool qux = false;
         Assert.AreEqual((foo ^ bar), false);
         Assert.AreEqual((foo ^ baz), true);
         Assert.AreEqual((baz ^ qux), false);
         Assert.AreEqual((foo ^ baz ^ qux), true);
         Assert.AreEqual((foo ^ bar ^ baz ^ qux), false);
      }
   }
}

 
 

This is one more thing I learned in the thousand page trip through C# 4.0 in a Nutshell by Joseph and Ben Albahari.

No comments:

Post a Comment