Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. adding chars

adding chars

Scheduled Pinned Locked Moved C#
help
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    humayunlalzad
    wrote on last edited by
    #1

    char a = 'A';
    char b = 'B';

    a = a + b; //error
    a += b; //OK

    I thought they r both exactly the same thing. Can someone tell me whats the difference.Pl.

    M W 2 Replies Last reply
    0
    • H humayunlalzad

      char a = 'A';
      char b = 'B';

      a = a + b; //error
      a += b; //OK

      I thought they r both exactly the same thing. Can someone tell me whats the difference.Pl.

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      i think... a = a + b; is trying to assign a string (a + b) to a char (a) where as.. a += b; is taking them to be int values and adding 65 + 66 (ASCII values) and setting a to be 131 Atleast this is my understading

      My opinion is... If someone has already posted an answer, dont post the SAME answer

      L 1 Reply Last reply
      0
      • M musefan

        i think... a = a + b; is trying to assign a string (a + b) to a char (a) where as.. a += b; is taking them to be int values and adding 65 + 66 (ASCII values) and setting a to be 131 Atleast this is my understading

        My opinion is... If someone has already posted an answer, dont post the SAME answer

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        5'ed, I think that's correct but without looking it up I can't be sure. That makes the most sense.

        1 Reply Last reply
        0
        • H humayunlalzad

          char a = 'A';
          char b = 'B';

          a = a + b; //error
          a += b; //OK

          I thought they r both exactly the same thing. Can someone tell me whats the difference.Pl.

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          The meaning of + operator depends on the types added together. "A char can be implicitly converted to ushort, int, uint, long, ulong, float, double, or decimal. However, there are no implicit conversions from other types to the char type" For chars the + is adding the integers together that are used for representing a single char. So in your first line the result is an int. The second line contains automatically assignment so the result type cannot change (since you don't have an intermediate result) thus the operation results to a char. In the line having an error you must do casting explicitely to the intermediate result.

          The need to optimize rises from a bad design.My articles[^]

          H 1 Reply Last reply
          0
          • W Wendelius

            The meaning of + operator depends on the types added together. "A char can be implicitly converted to ushort, int, uint, long, ulong, float, double, or decimal. However, there are no implicit conversions from other types to the char type" For chars the + is adding the integers together that are used for representing a single char. So in your first line the result is an int. The second line contains automatically assignment so the result type cannot change (since you don't have an intermediate result) thus the operation results to a char. In the line having an error you must do casting explicitely to the intermediate result.

            The need to optimize rises from a bad design.My articles[^]

            H Offline
            H Offline
            humayunlalzad
            wrote on last edited by
            #5

            Thanx, that makes sense

            W 1 Reply Last reply
            0
            • H humayunlalzad

              Thanx, that makes sense

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              You're welcome.

              The need to optimize rises from a bad design.My articles[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups