Francis Ducharme
Francis Ducharme

Reputation: 4987

How to do this on one line?

I am looking at tidying up this a bit but I'm kind of new to C#

ResponseList responsesList = new ResponseList();
PagedResponseList pagedResponsesList = new PagedResponseList();
responsesList = responseService.ListSurveyResponses(1000);
pagedResponsesList = responsesList.ResultData;

This is probably an easy one, but the syntax needed to one-line this one escapes me.

Upvotes: 3

Views: 121

Answers (4)

jdmcnair
jdmcnair

Reputation: 1315

As others have suggested, you can put it all into one line like so:

var pagedResponsesList = responseService.ListSurveyResponses(1000).ResultData;

But I think, given that you're calling off to another service, you'll really want to do some exception handling, check your result for null before referencing .ResultData, etc.

Upvotes: 1

BoltClock
BoltClock

Reputation: 723578

Firstly, you don't actually need the two new statements in your first two lines, as those new instances will be overwritten by whatever you assign in your last two lines anyway.

If you're new to C#, I suggest sticking with two lines at minimum so you understand at least what's happening in steps. In particular, the last two lines:

ResponseList responsesList = responseService.ListSurveyResponses(1000);
PagedResponseList pagedResponsesList = responsesList.ResultData;

You can then condense those two lines into the following line if you won't be using responsesList later in your code, and you think it'll be easy to understand (basically, just chain the .ResultData property behind responseService.ListSurveyResponses()):

PagedResponseList pagedResponsesList = responseService.ListSurveyResponses(1000).ResultData;

Upvotes: 7

General Grey
General Grey

Reputation: 3688

PagedResponseList pagedResponsesList=responseService.ListSurveyResponses(1000).ResultData

Upvotes: 0

millimoose
millimoose

Reputation: 39950

var pagedResponsesList = responseService.ListSurveyResponses(1000).ResultData;

Upvotes: 5

Related Questions