locked
What does the ^ mean in C#

    Question

  • using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace CheckBoxTest
    {
       public partial class Form1 : Form
       {
          public Form1()
          {
             InitializeComponent();
          }
    
          private void boldCheckBox_CheckedChanged(object sender, EventArgs e)
          {
             testLabel.Font = new Font(testLabel.Font.Name, testLabel.Font.Size,
                testLabel.Font.Style ^ FontStyle.Bold);
          }
    
          private void italicCheckBox_CheckedChanged(object sender, EventArgs e)
          {
             testLabel.Font = new Font(testLabel.Font.Name, testLabel.Font.Size,
                testLabel.Font.Style ^ FontStyle.Italic);
          }
       }
    }

    this is all the code

    I have no idea what does the character ^ mean.

    private void italicCheckBox_CheckedChanged(object sender, EventArgs e)
    {
       testLabel.Font = new 
          Font(testLabel.Font.Name,testLabel.Font.Size,
          testLabel.Font.Style ^ FontStyle.Italic);
    }

    when I replace it with a simple code:

    testLabel.Font = new Font(testLable.Font.Name, testLable.Font.Size, FontStyle.Italic);

    it's doesn't work (it's work once and cannot change back to normal when the check box is unchecked)

    Bold, Italic, etc is read-only property, cannot direct set value for them in C# code like other property, so when i want to set their value, I have to construct new Font object, and set value in constructor.

    So again, what does the character "^" mean?

    Thursday, February 16, 2012 3:31 AM

Answers

All replies

  • Logical XOR

    XOR = eXclusive OR

    http://msdn.microsoft.com/en-us/library/6a71f45d(v=vs.80).aspx

    In this context it means to set Italic if it is not already set.


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by Tran Van Tuan Friday, February 17, 2012 7:46 AM
    Thursday, February 16, 2012 4:16 AM
  • Little precision here...

    In this context it mean "Toggle italic"...(set it to Off if it is On, set it to On if its Off)

    Set italic to On if not already set would be | (logical Or)


    • Edited by Raevean Thursday, February 16, 2012 4:47 AM
    Thursday, February 16, 2012 4:47 AM
  • You are right...boolean side of my brain is apparently not functioning. Still an exclusive or though ;)

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Thursday, February 16, 2012 4:50 AM
  • Hello,

    Follow the links below.

    http://msdn.microsoft.com/en-us/library/csw1x2a6%28v=vs.71%29.aspx

    http://weblogs.asp.net/alessandro/archive/2007/10/02/bitwise-operators-in-c-or-xor-and-amp-amp-not.aspx

    Thursday, February 16, 2012 5:17 AM