Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When passing a file name to a method, should I use FileInfo or a plain file name?

Tags:

c#

fileinfo

Well, the title says it all. When passing a file name to a method, should I use a FileInfo object or a plain file name (string)? Why would I prefer one to the other?

Some of my colleagues like to write method like this:

  • void Export(FileInfo fileInfo)

Is it better than:

  • void Export(string fileName)

Thanks!

like image 669
Martin Avatar asked May 26 '09 20:05

Martin


2 Answers

I'd usually just use a string - it's simpler in most cases. Otherwise you're likely to just create a new FileInfo from the string in the first place.

If you're creating the method, you could always provide overloads to allow both.

Of course, if you know that where you're intending to call it, you usually have a FileInfo rather than a string, that's a different matter.

I can see your colleagues' point of view - in some ways a FileInfo is a "cleaner" way of expressing the parameter. I think string is the more pragmatic approach though :)

like image 69
Jon Skeet Avatar answered Nov 08 '22 07:11

Jon Skeet


Typically I would pass the string. However, you could overload the method to make everyone happy.

like image 35
Inisheer Avatar answered Nov 08 '22 09:11

Inisheer