hugerth
hugerth

Reputation: 171

Proper approach to interfaces in C#

I've created an interface which looks like this:

interface ICsvReader
{
    List<string> ReadFromStream(Stream csvStream);
}

My question is about return type List<string>. In tutorials I can see a lot of examples where methods are just void. In that cases interface looks natural:

interface ILogger
{
    void LogError(string error);
}

you don't have any specific destination of logging or method how to log errors. Like I said it looks natural for me, but what about specific types to return? Isn't it bad approach? When I'm using interface I want to create some abstraction over my methods - 'You should do this, but I don't care how'. So do you have any better idea for interface for file reader or something? I would like to read CSV from different sources but always return List<string>. Good or bad approach?

Upvotes: 0

Views: 161

Answers (3)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73442

Logger is kind of writer so void; ICsvReader as name suggests it is reader meaning it is going to read something for you and give it in return.

Have you ever seen a read method with return type void? I can't remember one!

Only thing I can suggest is use IEnumerable<string> Always promise less than what you can deliver. That will help you to switch to deferred execution if required in future.

There is nothing wrong here. Since Logger does write operation it is void that's not your case you need to yield something saying "this is what I read for you".

Upvotes: 3

Dnyanesh
Dnyanesh

Reputation: 2343

Regarding void vs List return type in interface

I think what approach you are taking is absolutely correct. In your case you are returning List is not incorrect, actually that is need of your application. And to do so your are declaring interface. Interface method declaration can be anything that suits your code.

As many answers suggested here for code optimization purpose use IEnumerable.

From Question:

So do you have any better idea for interface for file reader or something?

Just suggestion, do you really need to create interface. Because definition of your ReadFromStream method in your case looks like going to be same, so you may end up writing same code in various classes. And solution will be write method in base class/ in abstract class(in which you will achieve abstraction)

Upvotes: 0

Joker_vD
Joker_vD

Reputation: 3765

Well, returning List<string> means that you have the whole structure in your memory. For CSV files larger that 2 G this may be not appropriate.

Another choice would be returning IEnumerable<string> — that would let a CSV-reader to decide whether it want to read the whole file at once, or do incremental loading and parsing. Or you would be able to have two different classes, one that would try to load whole file at once, and another would work step-by-step.

Of course, List<T> has methods and properties that IEnumerable<T> doesn't have, so you would have to decide whether this added flexibility is worth it. But I've seen a number of server-side plugins that would read gigantic files into memory in order to send them to the client, so I recommend at least think about this.

Upvotes: 1

Related Questions