Results 1 to 4 of 4

Thread: [RESOLVED] Difference between = and ==

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Resolved [RESOLVED] Difference between = and ==

    I'm coming from a visual basic.net background. So....

    What's the difference between = and == for example in vb.net I'd do this:

    vb.net Code:
    1. If TextBox1.Text.Contains(".") = False Then

    but in C# it's requiring me to do this:
    c# Code:
    1. if (textbox1.text.containts(".") == False)

    why's that?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Difference between = and ==

    In VB, = is both the assignment operator and the equality operator. In C#, = is the assignment operator and == is the equality operator.

    This comes from the fact that C# syntax is based on C/C++ and, in those languages, assignments return a value. In C/C++ you can do something like this:
    Code:
    if (a = b)
    {
        // do something
    }
    else
    {
        // do something else
    }
    To a VB programmer, that looks like it's checking whether 'a' is equal to 'b' and performing one of two actions based on whether it is or not. That is not what's doing at all. Inside the parentheses is an assignment. It is assigning the value of 'b' to 'a'. As I said, unlike in VB, that assignment returns a value, i.e. the value that was assigned. The 'if' statement then implicitly converts that value to a Boolean and decides the action to perform based on whether it is true or false. In that conversion, zero or null become false and anything else become true.

    That's why you need separate assignment and equality operators in C-based languages. If you actually do want to check whether 'a' is equal to 'b' then you need to do this:
    Code:
    if (a == b)
    {
        // do something
    }
    else
    {
        // do something else
    }
    That can a bit of a trap in some C-based languages but C# is strictly-typed so it protects you from that in most cases. What that means is that, unlike in C/C++ and some other languages, you can't pass just anything to an 'if' statement and have it implicitly converted to a Boolean value. If you accidentally use = instead of == in C/C++ then the code will end up doing something other than what you intend. If you do it in C# then it will be flagged as a syntax error unless the expression explicitly evaluates to type Boolean. Anything else will not be implicitly converted.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Difference between = and ==

    By the way, I wouldn't use the code you showed in either case. What you're actually doing is evaluating one expression as a Boolean, comparing that to a Boolean literal and producing a third Boolean, then testing that. If the Contains method itself return a Boolean then you can just test that. If you want to test whether it's False then you use the fundamental rule of Boolean algebra that 'Not True' is False:
    vb.net Code:
    1. If Not TextBox1.Text.Contains(".") Then
    csharp Code:
    1. if (!textBox1.Text.Contains("."))

  4. #4

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Difference between = and ==

    Thanks for explaining the difference for me John, as well as suggesting the Not(or ! in C#). For whatever reasons I was having problems wrapping my head around that.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width