简体   繁体   中英

Should I instantiate variable that will hold the return type from a method call?

I have a CarSpecs class and I have a service with a method called GetCarSpecs who's return type is of CarSpecs. Should I call the method like this:

CarSpecs carSpecs = service.GetCarSpecs();

Or is there a reason why it would make sense to instantiate the CarSpecs class first before calling the method in the service like this:

CarSpecs carSpecs = new CarSpecs();
carSpecs = service.GetCarSpecs();

No, there is no need to instantiate an additional object that wil just be discarded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM