Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Should the default value of an enum be None or Unknown?

Say you have an enum that represents an error code. There will be several codes, each with their own underlying int value; however, the enum value that gets the default 0 value seems like it should be carefully considered.

In the case of an error code enum, there are two special values that I can think of: None (for cases when there is no error) and Unknown (for cases when no existing error code is appropriate, or perhaps even when error state cannot be detected).

One of these values seems like it should get 0, where the other will probably get something else like -1. Is it more appropriate to set the None value to 0, or the Unknown value to 0?

public enum ErrorCode
{
    None = -1,
    Unknown = 0,
    InsufficientPermissions,
    ConnectivityError,
    ...
}

public enum ErrorCode
{
    Unknown = -1,
    None = 0,
    InsufficientPermissions,
    ConnectivityError,
    ...
}

My instinct tells me that the default should be Unknown, but I'm curious if anyone has done it differently.

like image 631
bwerks Avatar asked Jul 01 '10 17:07

bwerks


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What are basics of C?

It was mainly developed as a system programming language to write an operating system. The main features of the C language include low-level memory access, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like an operating system or compiler development.


2 Answers

Since you're tagging your question with best-practices: don't use error codes when you can use exceptions.

like image 177
Pontus Gagge Avatar answered Sep 23 '22 20:09

Pontus Gagge


In my opinion both Unknown or None mean the same thing in the context of the ErrorCode enumeration. My reasoning is that if I'm checking an error code than it's because I already have an error.

I'm also of the opinion that an error code enumeration is only useful in a custom exception or as custom data of an existing exception type and in both scenarios you will always have an error.

like image 38
João Angelo Avatar answered Sep 24 '22 20:09

João Angelo