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. struct question

struct question

Scheduled Pinned Locked Moved C#
question
10 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.
  • A Offline
    A Offline
    akkram
    wrote on last edited by
    #1

    Hi, im trying to get my head around structs. Whats the difference between using the following? public struct aStruct { public int sId; }; static void Main(string[] args) { Program prog = new Program(); aStruct theStruct; theStruct.sId = 5; or aStruct theStruct = new aStruct(); theStruct.sId = 5; }

    E 1 Reply Last reply
    0
    • A akkram

      Hi, im trying to get my head around structs. Whats the difference between using the following? public struct aStruct { public int sId; }; static void Main(string[] args) { Program prog = new Program(); aStruct theStruct; theStruct.sId = 5; or aStruct theStruct = new aStruct(); theStruct.sId = 5; }

      E Offline
      E Offline
      Ed Poore
      wrote on last edited by
      #2

      Nothing really, the most important thing with structs is they're value types, i.e. if you did the following:

      aStruct firstStruct = new aStruct();
      firstStruct.sId = 5;
      aStruct secondStruct = firstStruct;

      then secondStruct contains a copy of firstStruct not a reference to it, i.e. you can change properties on secondStruct without changing firstStruct.  If this were a class then changing secondStruct would change firstStruct as well. To be honest I would use the second method because it is more explicit and clearer what you are trying to do, if you then decide to change the declaration of aStruct to a class rather than a struct then you don't have lots of NullReferenceExceptions popping up all over the code.

      A 1 Reply Last reply
      0
      • E Ed Poore

        Nothing really, the most important thing with structs is they're value types, i.e. if you did the following:

        aStruct firstStruct = new aStruct();
        firstStruct.sId = 5;
        aStruct secondStruct = firstStruct;

        then secondStruct contains a copy of firstStruct not a reference to it, i.e. you can change properties on secondStruct without changing firstStruct.  If this were a class then changing secondStruct would change firstStruct as well. To be honest I would use the second method because it is more explicit and clearer what you are trying to do, if you then decide to change the declaration of aStruct to a class rather than a struct then you don't have lots of NullReferenceExceptions popping up all over the code.

        A Offline
        A Offline
        akkram
        wrote on last edited by
        #3

        Thx alot, so say for example if i wrote the following: public struct aStruct { public int sId; }; static void Main(string[] args) { Program prog = new Program(); aStruct theStruct; ArrayList list = new ArrayList(); theStruct.sId = 5; list.Add(theStruct); theStruct.sId = 6; list.Add(theStruct); theStruct.sId = 7; list.Add(theStruct); } I would then add three objects of type theStruct to the list even though I didn't explicitly created them with new?

        E P 2 Replies Last reply
        0
        • A akkram

          Thx alot, so say for example if i wrote the following: public struct aStruct { public int sId; }; static void Main(string[] args) { Program prog = new Program(); aStruct theStruct; ArrayList list = new ArrayList(); theStruct.sId = 5; list.Add(theStruct); theStruct.sId = 6; list.Add(theStruct); theStruct.sId = 7; list.Add(theStruct); } I would then add three objects of type theStruct to the list even though I didn't explicitly created them with new?

          E Offline
          E Offline
          Ed Poore
          wrote on last edited by
          #4

          Yes

          A 1 Reply Last reply
          0
          • E Ed Poore

            Yes

            A Offline
            A Offline
            akkram
            wrote on last edited by
            #5

            ok, clear as day

            1 Reply Last reply
            0
            • A akkram

              Thx alot, so say for example if i wrote the following: public struct aStruct { public int sId; }; static void Main(string[] args) { Program prog = new Program(); aStruct theStruct; ArrayList list = new ArrayList(); theStruct.sId = 5; list.Add(theStruct); theStruct.sId = 6; list.Add(theStruct); theStruct.sId = 7; list.Add(theStruct); } I would then add three objects of type theStruct to the list even though I didn't explicitly created them with new?

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              While Ed has given you the correct answer here, you should really try this out for yourself rather than relying on others. This is down to the fact that sometimes people give answers without really knowing what they are talking about. (Note - Ed is one of the ones who DOES know what he is talking about). Never accept that you have been given the correct answer. Try it out, and make sure that it is the correct answer.

              Deja View - the feeling that you've seen this post before.

              E 1 Reply Last reply
              0
              • P Pete OHanlon

                While Ed has given you the correct answer here, you should really try this out for yourself rather than relying on others. This is down to the fact that sometimes people give answers without really knowing what they are talking about. (Note - Ed is one of the ones who DOES know what he is talking about). Never accept that you have been given the correct answer. Try it out, and make sure that it is the correct answer.

                Deja View - the feeling that you've seen this post before.

                E Offline
                E Offline
                Ed Poore
                wrote on last edited by
                #7

                Pete O`Hanlon wrote:

                Note - Ed is one of the ones who DOES know what he is talking about

                Well that's nice to know :-O


                My Blog[^]

                P P 2 Replies Last reply
                0
                • E Ed Poore

                  Pete O`Hanlon wrote:

                  Note - Ed is one of the ones who DOES know what he is talking about

                  Well that's nice to know :-O


                  My Blog[^]

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  Well, credit where credit is due.:-D

                  Deja View - the feeling that you've seen this post before.

                  1 Reply Last reply
                  0
                  • E Ed Poore

                    Pete O`Hanlon wrote:

                    Note - Ed is one of the ones who DOES know what he is talking about

                    Well that's nice to know :-O


                    My Blog[^]

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    But now you have to live up to your reputation.

                    E 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      But now you have to live up to your reputation.

                      E Offline
                      E Offline
                      Ed Poore
                      wrote on last edited by
                      #10

                      Damn! :doh:

                      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