Everyone touched on the obvious, so here is another option.
If your class needs to be initialized in any way (your SomeObject needs to be installed at some point), consider implementing ISupportInitialize . This tells the users of your class that it is not in the correct state until EndInit() called. Unfortunately, there is no existing generic NotInitializedException that you can use (some of them are specific to specific namespaces), so I would suggest linking your interface implementation to throwing such an exception.
First, users must call BeginInit() , then configure the instance, and then call EndInit() . When calling EndInit() check the status of your instance. If it is incorrectly initialized, you can raise an InvalidOperationException. If users try to use your instance before initialization, you should throw a NotInitializedException.
Itโs a bit bigger, but the advantage is that you create a wider โpit of successโ for your users, ensuring that they use your classes correctly, quickly and early, and by the very definition of your type (the interface is pretty clear that you expect). It also gives you more โroomโ, so to speak, to document how your class should be used.
Will
source share