Function return type question

mp3909

Well-known member
Joined
Apr 22, 2018
Messages
61
Location
UK
Programming Experience
3-5
In the below code which I got from the Internet, function has return type as IList<String> but the function actually returns studenList which is declared as var.
So I am bit confused how that works?

C#:
private static IList<String> FindAllStudentFromDatabase(string studentName)
{
    var studentList = // find all students with same name from the database

    return studentList;
}
 
Presumably, the part that you commented out ends with something like .ToList(). The var keyword does type inference based on the result of the right hand side. So if the assumption is correct, then the type for studentList will be a List<T>.

List<T> implements the interface IList<T>. (See the documentation.) They are following good object oriented practices that say that you should program against interfaces. These are the "L" and "I" parts of the SOLID object oriented programming principles.

If the assumption above about the existence of .ToList() is wrong, whatever is returned by the right hand side is something that implements IList<T>.
 
Last edited:
Back
Top Bottom