Using Enumerations as Status Indicators in C #

Just wondering what the general opinion was about returning enumerations from methods indicating status. The notion of return codes (ints) is rather inherent in programming school systems (C), but I wonder if the methodology for determining status has changed.

thanks

Edit: I know that enums are basically int values. I ask about pure practice when methods start throwing status codes (whether they are enumerations or ints) to indicate statuses in general. This seems like good practice, but I was wondering if there were any negative feelings for this idea.

I would always choose an int return listing for the status code.

+3
source share
7 answers

If you return enumerations to indicate errors and not to throw exceptions, you are, of course, in a state of sin.

Are callers really interested in the state of something? I usually found these things in C-style function libraries where there are no objects to represent state. It depends on what you are trying to do, but if you (say) create code to manage communication sockets, I highly recommend wrapping them in classes and letting client code determine their state through properties, and not as a result of the Open () method.

+7
source

, , - , - , , , , 1 , .

+3

- int, . intellisense.

, . / , ().

+1

( ..) ( , , ) - , , C. .

, # -Style () . . , , . usefule, ?

+1

, (), , , . , , .

+1

Pontus Gagge : .

, : . :

public enum Status
{
    Good,
    Bad,
    Indifferent
};

public class Program
{
    static void Main(string[] args)
    {
        Status s = (Status) 30;
    }
}

, ; .

0

I thought about it (and worked a little), and I found it more useful that I simply returned a boolean, an integer, or went all the way to creating a class to return a state and some values ​​associated with that state, and I really find practical returning a simple enumerations : my code seems to be more readable, and I don’t have the whole burden of exception, because when I read one: the exception should be exceptional =)

0
source

Source: https://habr.com/ru/post/1704368/


All Articles