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. String, Why does this work??

String, Why does this work??

Scheduled Pinned Locked Moved C#
questionhelplearning
5 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.
  • G Offline
    G Offline
    GrooverFromHolland
    wrote on last edited by
    #1

    Hi all,

    y = 2;
    string yString = y;

    This of course does not work ,Cannot implicitly convert type 'int' to 'string'. But this is no problem:

    y = 2;
    string yString = "" + y;

    Why?? Regards, Grooover

    0200 A9 23 0202 8D 01 80 0205 00

    B A 2 Replies Last reply
    0
    • G GrooverFromHolland

      Hi all,

      y = 2;
      string yString = y;

      This of course does not work ,Cannot implicitly convert type 'int' to 'string'. But this is no problem:

      y = 2;
      string yString = "" + y;

      Why?? Regards, Grooover

      0200 A9 23 0202 8D 01 80 0205 00

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      Because the designers of .NET C# decided that the "+" operator between strings ... if the item before the plus-sign is a string ... would automatically invoke conversion of all items to the right of the plus-sign to Type String. You can see an even more dramatic example of this if you execute:

      int v = 1;
      Int32 w = 2;
      Int64 x = 3;
      double y = 4;
      float z1 = 5;
      Single z2 = 6;
      string yString = "" + v + w + x + y + z1 + z2;

      Yep, it compiles. So, you can infer that ... given a lack of parentheses) arithmetical operator evaluation for the plus sign is "turned off." If you think this is kind of strange, you may not be alone :)

      “I speak in a poem of the ancient food of heroes: humiliation, unhappiness, discord. Those things are given to us to transform, so that we may make from the miserable circumstances of our lives things that are eternal, or aspire to be so.” Jorge Luis Borges

      G R 2 Replies Last reply
      0
      • B BillWoodruff

        Because the designers of .NET C# decided that the "+" operator between strings ... if the item before the plus-sign is a string ... would automatically invoke conversion of all items to the right of the plus-sign to Type String. You can see an even more dramatic example of this if you execute:

        int v = 1;
        Int32 w = 2;
        Int64 x = 3;
        double y = 4;
        float z1 = 5;
        Single z2 = 6;
        string yString = "" + v + w + x + y + z1 + z2;

        Yep, it compiles. So, you can infer that ... given a lack of parentheses) arithmetical operator evaluation for the plus sign is "turned off." If you think this is kind of strange, you may not be alone :)

        “I speak in a poem of the ancient food of heroes: humiliation, unhappiness, discord. Those things are given to us to transform, so that we may make from the miserable circumstances of our lives things that are eternal, or aspire to be so.” Jorge Luis Borges

        G Offline
        G Offline
        GrooverFromHolland
        wrote on last edited by
        #3

        Thank You for your explanation, Learn something every day. thanks

        0200 A9 23 0202 8D 01 80 0205 00

        1 Reply Last reply
        0
        • G GrooverFromHolland

          Hi all,

          y = 2;
          string yString = y;

          This of course does not work ,Cannot implicitly convert type 'int' to 'string'. But this is no problem:

          y = 2;
          string yString = "" + y;

          Why?? Regards, Grooover

          0200 A9 23 0202 8D 01 80 0205 00

          A Offline
          A Offline
          Alan N
          wrote on last edited by
          #4

          The concatenation operators are replaced by calls to one of the overloaded string.Concat methods. There are versions with string or object parameters, e.g Concat(string, string, string), Concat(object, object, object), and if possible the more efficient (string, string, ...) versions will be used. This gives us, well me really, the opportunity to play with ILDASM and have a look at what the C# compiler decides to do.

          private static String StringConcat(String a, String b, String c) {
          return a + b + c;
          }

          private static String ObjectConcat(String a, int b, DateTime c) {
          return a + b + c;
          }

          and the IL shows the use of the different Concat methods.

          .method private hidebysig static string StringConcat(string a,
          string b,
          string c) cil managed
          {
          // Code size 9 (0x9)
          .maxstack 8
          IL_0000: ldarg.0
          IL_0001: ldarg.1
          IL_0002: ldarg.2
          IL_0003: call string [mscorlib]System.String::Concat(string,
          string,
          string)
          IL_0008: ret
          } // end of method ConcatTest::StringConcat

          .method private hidebysig static string ObjectConcat(string a,
          int32 b,
          valuetype [mscorlib]System.DateTime c) cil managed
          {
          // Code size 19 (0x13)
          .maxstack 8
          IL_0000: ldarg.0
          IL_0001: ldarg.1
          IL_0002: box [mscorlib]System.Int32
          IL_0007: ldarg.2
          IL_0008: box [mscorlib]System.DateTime
          IL_000d: call string [mscorlib]System.String::Concat(object,
          object,
          object)
          IL_0012: ret
          } // end of method ConcatTest::ObjectConcat

          Alan.

          1 Reply Last reply
          0
          • B BillWoodruff

            Because the designers of .NET C# decided that the "+" operator between strings ... if the item before the plus-sign is a string ... would automatically invoke conversion of all items to the right of the plus-sign to Type String. You can see an even more dramatic example of this if you execute:

            int v = 1;
            Int32 w = 2;
            Int64 x = 3;
            double y = 4;
            float z1 = 5;
            Single z2 = 6;
            string yString = "" + v + w + x + y + z1 + z2;

            Yep, it compiles. So, you can infer that ... given a lack of parentheses) arithmetical operator evaluation for the plus sign is "turned off." If you think this is kind of strange, you may not be alone :)

            “I speak in a poem of the ancient food of heroes: humiliation, unhappiness, discord. Those things are given to us to transform, so that we may make from the miserable circumstances of our lives things that are eternal, or aspire to be so.” Jorge Luis Borges

            R Offline
            R Offline
            Rob Philpott
            wrote on last edited by
            #5

            Thing of beauty, .NET.

            Regards, Rob Philpott.

            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