Reputation: 5090
I have the classes :
public class Result{
public string Name {get; set;}
public string Category {get; set;}
}
public SearchResults {
public string PageNum {get; set;}
public string PageSize {get; set;}
public string TotalCount {get; set;}
public IEnumerable<Result> Results {get; set;)
}
I have the method:
public SearchResults GetSearchResults(bool full = false)
{
return new SearchResults {
PageNum = 1,
PageSize = 25,
TotalCount = 100,
Results = new[] {new Result {Name="Alpha", Category="Bravo"}}
// return Results only if parameter [full] is true ???How
};
}
In the above method, I want to return [Results] only if the [full] parameter is true. And by default not return it.
Upvotes: 0
Views: 35
Reputation: 223292
You can use Conditional operator and return null
for result like:
Results = full ? new[] { new Result { Name = "Alpha", Category = "Bravo" } } : null,
By default since Result
is a property it will be assigned null
(default).
So your method would be like:
public SearchResults GetSearchResults(bool full = false)
{
return new SearchResults
{
PageNum = 1,
PageSize = 25,
TotalCount = 100,
Results = full ? new[] { new Result { Name = "Alpha", Category = "Bravo" } } : null,
};
}
Upvotes: 1