struct question
-
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; }
-
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; }
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 offirstStruct
not a reference to it, i.e. you can change properties onsecondStruct
without changingfirstStruct
. If this were a class then changingsecondStruct
would changefirstStruct
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 ofaStruct
to a class rather than astruct
then you don't have lots ofNullReferenceException
s popping up all over the code. -
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 offirstStruct
not a reference to it, i.e. you can change properties onsecondStruct
without changingfirstStruct
. If this were a class then changingsecondStruct
would changefirstStruct
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 ofaStruct
to a class rather than astruct
then you don't have lots ofNullReferenceException
s popping up all over the code.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?
-
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?
-
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?
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.
-
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.
-
Well, credit where credit is due.:-D
Deja View - the feeling that you've seen this post before.
-
But now you have to live up to your reputation.
-
But now you have to live up to your reputation.