Any difference between returning an object and changing an object passed into a static function? [modified]
-
I was wondering if there was any difference between the following code:
public void LoadAll() { obj x; //static function that will change the object StaticClass.DoSomething(x); }
public void LoadAll() { //static function that will return a new object obj x = StaticClass.DoSomething(); }
Are there any pros and cons to either of these? Are there any concerns with thread safety? -- modified at 22:55 Sunday 3rd June, 2007 -
I was wondering if there was any difference between the following code:
public void LoadAll() { obj x; //static function that will change the object StaticClass.DoSomething(x); }
public void LoadAll() { //static function that will return a new object obj x = StaticClass.DoSomething(); }
Are there any pros and cons to either of these? Are there any concerns with thread safety? -- modified at 22:55 Sunday 3rd June, 2007Any concerns you may have remain the same in either case, ultimately I'd go with the second for clarity, but so long as it's all running in the same thread, which it is here, I don't see any thread safety concerns. The second approach does allow the method to create the object whenever it likes, the first means it always exists. The first would be better represented with the out keyword, seeing as you expect the value to be assigned, and not just read. Stupid C# doesn't have const, but you can make your code clear by using the out keyword.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )