Why we have to put ToList To List Object in c#

Posted by Kasani007 under LINQ on 7/29/2016 | Points: 10 | Views : 400 | Status : [Member] | Replies : 2
Why we have to put ToList To List Object in c#

List<Student> students=GetAllStudentData().ToList(); 
//Domain Object

now, I had Mapped To Business Object

List<StudentBE> studentBE=MapDomainToBusiness(students)
// as the MapDomainToBusiness() method returns .ToList() Object

return studentBE.ToList();
// Again Is It Needed To use .ToList() at return time //Here ToList Is necessary or not


Please let me now the importance of ToList




Responses

Posted by: Sheonarayan on: 7/30/2016 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
It is not, by default List inherits IEnumerable interface that allows the collection to enumerate through each object into the collection.

However if you are assigning the return collection (that is IEnumerable<object> type) to List object again then you will need to convert the IEnumberable to List by calling .ToList().

Hope this helps.
Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kasani007 on: 8/2/2016 [Member] Starter | Points: 25

Up
0
Down
hi Sheo Narayan,

That Means Only IEnumerable Object Needs To Be Convert By .ToList()...ok

But A List Object Is Not Necessary To Convert Again .ToList() ...is it right

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response