Reputation: 1803
Is it OK to to declare a return type of a method, e.g. IList that is less than you define it internally, e.g. List? Will it compile and run?
protected static IList<String> GetJobFiles(String folder, String fileExtension)
{
List<String> jobFiles = new List<String>();
if (System.IO.Directory.Exists(folder))
{
string[] files = Directory.GetFiles(folder, fileExtension);
// Check if for matching files
if (files.Length > 0)
{
// add the files to our list collection
jobFiles.AddRange(files);
}
}
return jobFiles;
}
Upvotes: 2
Views: 7373
Reputation: 499002
This is not only fine, but good practice and the whole point of having interfaces as abstractions. See LSP
on wikipedia.
It allows you to use any implementer of IList<T>
(so, not just List<T>
, in case you want to change the internal implementation).
As for "will it compile and run" question - the answer is yes and yes, however, I encourage you to try for yourself. You will learn more and faster than posting here...
As @Dan Tao comments, try for yourself, but confirm (either here or with experienced colleagues).
Upvotes: 8
Reputation: 27608
Note that, depending on what the intended usage of the return value is, you might even choose to return IEnumerable:
protected static IEnumerable<String> GetJobFiles(String folder, String fileExtension)
This is even more general and you could use many types internally to build up the list (collection, queue, stack, or just an enumerator using yield) without impacting the caller.
In this case, the caller of the function can loop over the "job files", use the enumerable in linq (which he can also do with IList).
The caller cannot modify the list (he cannot add or remove files).
Upvotes: 1
Reputation: 128327
The IList<string>
interface is implemented by both List<string>
and string[]
.
So you could return jobFiles
, or, let's say down the road you decide that's not necessary and you want to just return files
directly.
The fact that you've defined your method as returning an implementation of IList<string>
rather than a List<string>
allows you to do this without affecting any external code*. This is in fact one of the main purposes of using interfaces.
*This is assuming you would not be treating the return value as a mutable collection using, e.g., Add
, Insert
, etc. Otherwise, you would in fact be affecting external code; but this is in theory at least one of the benefits of interfaces.
Upvotes: 4
Reputation: 101604
Yes, that's the purpose of an interface. You are specifying that something that implements the list interface will be returned. If you wanted to have several classes (Say Dog, Cat, Bird, etc) that all implemented IAnimal (Say had the Feed() and Bath() methods) you could have a function return an IAnimal without being specific as to which type of animal you'd get back.
Upvotes: 0
Reputation: 32447
Since IList is more "general" than List (in that List implements the IList interface), then this is a perfectly good way to do things.
Upvotes: 0