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. Sharing a lesson...

Sharing a lesson...

Scheduled Pinned Locked Moved C#
comperformancequestionannouncement
3 Posts 3 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.
  • M Offline
    M Offline
    MarkB777
    wrote on last edited by
    #1

    Not a question, but something I learnt today that I'd like to share ;). If I declare and initialise as follows... string[] a,b,c; a = b = c = new string[size]; a, b, and c all share the same memory location (i.e if you alter b - c and a are affected the same). Something I should have spotted, but is all so easy to overlook with these fancy new languages. :).

    Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen Click here to view my blog

    Z R 2 Replies Last reply
    0
    • M MarkB777

      Not a question, but something I learnt today that I'd like to share ;). If I declare and initialise as follows... string[] a,b,c; a = b = c = new string[size]; a, b, and c all share the same memory location (i.e if you alter b - c and a are affected the same). Something I should have spotted, but is all so easy to overlook with these fancy new languages. :).

      Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen Click here to view my blog

      Z Offline
      Z Offline
      zafersavas
      wrote on last edited by
      #2

      Exactly, because arrays in C# are reference types.

      1 Reply Last reply
      0
      • M MarkB777

        Not a question, but something I learnt today that I'd like to share ;). If I declare and initialise as follows... string[] a,b,c; a = b = c = new string[size]; a, b, and c all share the same memory location (i.e if you alter b - c and a are affected the same). Something I should have spotted, but is all so easy to overlook with these fancy new languages. :).

        Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen Click here to view my blog

        R Offline
        R Offline
        Robert C Cartaino
        wrote on last edited by
        #3

        Someone might be mislead to believe that... string[] a, b, c; a = b = c = new string[size]; would behave the same as... int a, b, c; a = b = c = 10; ...which (as you discovered) it doesn't.

        MarkBrock wrote:

        but is all so easy to overlook with these fancy new languages

        ...easy to overlook because it is poor style and easily misleading. To clearly show your intentions, it would have been better coded as either:

        string[] a = new string[size];
        string[] b = a;
        string[] c = a;

        or

        string[] a = new string[size];
        string[] b = new string[size];
        string[] c = new string[size];

        ...depending on what your intentions are.

        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