Search code examples
c#fileinfo

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


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!


Solution

  • 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 :)