I'm writing a wrapper class in C# for a USB device (using P/Invoke to communicate with it). The device needs to be sent an open message before use, and a close method when it's done with. In my constructor I want to try to open the device and return null
if that call fails for whatever reason. I'll also write a Dispose()
which calls the close method on the device.
Is this a sensible way to code it? Or should I have a separate ConnectToDevice
method?
By putting it in the constructor I hope users of my class can simply write:
USBDevice myDevice = new USBDevice()
if (myDevice != null) {
myDevice.PerformAction();
myDevice.Dispose();
myDevice = null;
}