Keep in mind that .NET Framework is in unique position - it is a set of core, foundational classes used by millions of developers around the world. As such, it has some very tight constraints in terms of performance, error handling, etc. For example, the traditional way to do argument validation is to validate arguments on entry to every method. However, sometimes you have a bunch of methods that end up calling each other, so arguments get re-validated all the time. Most often this isn't a problem, but when your method is e.g. on System.String
(just an example), so it's called often from everywhere, those extra checks do add up very quick. So you have to refactor the code into a separate private method, or a bunch of methods, and have your public API perform validation only once on the API boundary, and delegate to those non-validating private methods. This complicates the code, but it gives the clients better experience.
The above was just an example, and there may be many other instances where micro-optimizations such as using ref
/out
, or avoiding lambdas (and using flags and other means of communicating state), is needed because the framework class/method is found from experience to be on a "hot path" often. This leads to "overcomplicated" code that you observe.
Another issue is with rare corner cases. Again, when you have those millions of developers throwing all kinds of input at your APIs and expecting them to handle all that, you start running into some very non-obvious corner cases for that input that often have to be handled in a very special way. To see what I mean, have a look at the source code for System.Uri
- preferably not in Reflector, but the version that comes from .NET Framework Source Server, which comes with comments documenting why it does some very non-obvious things that it does.
In general, when writing libraries, it is almost always some trade-off between convenience (and development time) for the library authors, and convenience (and performance) for library clients. The wider your audience, the more pressed you are to shift the balance in favor of the clients.