I have got following code:
I have a enum:
public enum HardwareInterfaceType
{
Gpib = 1,
Vxi = 2,
GpibVxi = 3,
}
and m using this enum like this :
HardwareInterfaceType type = default(HardwareInterfaceType);
please tell me what is keyword “default” is doing here? what is the use of it?
I believe the default is 0, so you will have an invalid HardwareInterfaceType generated. I personally don’t like this sort of coding for enums. IMO it’s more clear to define an Enum value of “Undefined” or “None” and then initialize the variable to that instead.
One of the “Gotchas” with enums is that they can be initialized to a value that does not exist in the enum. The original programmer might have thought that the enum would be initialized to some valid value in the Enum definition if he used the “default” keyword. Not true, and IMO this is not a good use of the “default” keyword at all.