I am adding an object to a list and then afterwards setting the object variable to null. To add the another object to the list do i need to create a new object. The code I am using is below:
UpdateData = new MismatchData();
UpdateData.CINID = currentInLoopCIN_ID;
UpdateData.ColumnMapID = <some integer>;
UpdateData.WMSValue = <some integer>;
lsMismatchData.Add(UpdateData);
UpdateData = null;
Do I need to have the first line?
UpdateData = new MismatchData(); //Do I need to have this line of code?
Since you’re setting
UpdateDateto null, you’d get an error when you’d try to setUpdateData.CINID, the second time around (you’d be accessing a null object).Your null assignment is the redundant line of code here. Since you’re always assigning a
new UpdateDatato your variable, you don’t need to reset it in between.With or without the null assignment, you’ll still need to create a new instance every time, though, otherwise you’d just be updating the old instance.
If you want to cut down on lines of code, you could do something like this:
Or, since it looks like you’re in a loop, you might be able to do something like this, to make your code even terser: