0

I have the below methods:

    public static byte[] ConvertFileToBytes(string filePath)
    {
        var fInfo = new FileInfo(filePath);
        var numBytes = fInfo.Length;
        var dLen = Convert.ToDouble(fInfo.Length / 1000000);

        var fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        var br = new BinaryReader(fStream);

        var data = br.ReadBytes((int)numBytes);
        br.Close();

        fStream.Close();
        fStream.Dispose();

        return data;
    }

    public static void ConvertBytesToFile(byte[] file, string filePath)
    {
        var ms = new MemoryStream(file);

        var fs = new FileStream(filePath, FileMode.Create);

        ms.WriteTo(fs);

        ms.Close();
        fs.Close();
        fs.Dispose();
    }

What is the correct to name these methods? (because ConvertXXXtoYYY just doesn't cut it in a Utilities library)

5 Answers 5

10

How about File.ReadAllBytes and File.WriteAllBytes ;)

3
  • ...haha! i just continue learning a ton of new things everyday. Cheers mate! Apr 26, 2009 at 21:30
  • Not trying to be a jerk but I have seen this a couple of times. This answer doesn't answer the actual question posed. It is a good suggestion though. The question is about his method signatures not implementation. Would thnk that SO would want to keep these somewhat consistent.
    – Chad Grant
    Apr 27, 2009 at 0:57
  • @Deviant: I think it does in that I'm inferring that the correct names should be "Read" and "Write". Since we're dealing specifically with files, "(de)serialize" is too generic. Same for "marshal", which I think also has slightly different connotations. Apr 27, 2009 at 7:05
2

The terms usually used are "serialize" and "deserialize" (or sometimes "marshal" and "demarshal").

1
  • To me, both "serialize" and "marshal" imply that you're dealing with some sort of data structure that needs special logic for converting it to bytes - but we already have bytes, they just need to be written to a file (which is nothing but a bunch of bytes as well). Jun 16, 2009 at 9:07
0

Marshalling/Unmarshalling might be the appropriate term.

http://en.wikipedia.org/wiki/Marshalling_(computer_science)

2
  • hmm, so what's the difference between marshalling and serializing? Apr 26, 2009 at 21:35
  • Marshalling is traditionally used when dealing with XML, serializing when dealing with in-memory data structures. Neither really applies to the simple task of writing bytes directly to a file. Jun 16, 2009 at 9:10
0

In C++ they would be called read and write.

0

The WriteAllBytes and ReadAllBytes are a good suggestion, but to answer your Question ...

Save() would be a good choice for renaming of ConvertToFile() and Object.CreateFromFile() for the reverse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.