I want to write a static utility class which only has a set of properties, which expose functionality to the user
For example I could call:
Utils.String.GetHexString("Hello World");
or
Utils.Stream.CopyBytes(instream, outstream);
The closest thing I could liken this to is System.Text.Encoding where there are properties like UTF8, ASCII etc, so yo can call things like:
Encoding.UTF8.GetBytes("Hello World");
or
Encoding.ASCII.GetBytes("Hello World");
The problem is that in Encoding, this calls the equivalent objects (UTF8Encoder, ASCIIEncoder) which are publicly available to the user. What I want is to expose the objects ONLY via Utils, without visibilty of the objects that relate to the properties, for example
I could call:
Utils.Stream.CopyStream(instream, outstream);
but I could not call:
StreamUtils.CopyStream(instr, outstr) //This class is only accessible via the Utils class!
Is this possible, and if it is, is it going to be good or bad practice to do so?
Ok, basically it’s no possible (in C# at least) to achieve exactly what I want. The class must be visible for the functionality to be visible through the Util class.
My solution was to turn the classes (e.g.
StreamUtils,StringUtils) into singletons. They cannot be constructed because their constructors are internal. their methods are public but can never be seen because the object cannot be instantiated outside of my assembly. The Utils class exposes the functionality to the user via the singleton instance.Consider the following: