Search code examples
c#linqlanguage-agnosticprogramming-languages

Semantic difference of Read and Load


I wonder what the semantic difference is between Read and Load (in C#). I don't see a difference when comparing e.g.

  • System.IO.MemoryStream.Read()
  • System.Console.Read()
  • System.IO.StreamReader.Read()
  • System.IO.File.ReadAllText()

vs

  • System.Xml.XmlDocument.Load()
  • System.Xml.Linq.XDocument.Load()
  • System.Reflection.Assembly.Load()

Since I want a consistent naming over my program that deals a lot with simply getting files from persistent storage and higher level functions that also initialize, cross reference and errorcheck I kindly ask for your input.


Solution

  • In your examples, "Read" generally refers to reading a portion of the data. Whether this is for the purpose of limiting the amount of data that needs to be stored and/or handled in a given operation, or because the data itself is not immediately available in its entirety (e.g. Console.Read() or reading from a network stream), the fundamental behavior is the same: data is processed in pieces smaller than the entire set of data that can or will be processed.

    There is the exception ReadAllText(), which does in fact read all of the data at once. But that's in a type where all the other methods that perform similarly also use the word "Read". Using "Read" in that context keeps the API consistent, and failing to use "Load" doesn't significantly hinder comprehension of the API (especially since the method name also explicitly states "All Text"…no one should be surprised to see all of the text read in that case, right? :) ).

    In your examples that use "Load", they consume all of the data at once, and turn it into something else, e.g. an XML DOM or an assembly. This is a distinctly different kind of operation from just reading data and at most doing minimal processing on it (e.g. decoding some text format). In contrast to "Read" operations, "Load" will always consume all of the data, rather than allowing the option of reading just a portion at a time.