C # enum add-on

Hi, I have the following listing:

public enum LegalShipTypes : byte { Frigate = 1, Cruiser = 2, Destroyer = 3, Submarine = 4, AircraftCarrier = 5 } 

I was wondering if there is a way to get the full value of the enum in any way. for example, this will result in (1 + 2 + 3 + 4 + 5) = 15.

thanks.

+4
source share
7 answers

I did not want to type this as an answer, because it does not directly answer your question, but based on your comment in response to my comment on your question, it deserves some explanation.

Enumerations should be very simple types of safe state representations. If you just use constants, then you can assign invalid constants to a value. This does not allow you to assign the wrong type of constant to the field. For example, if you have something that DayOfWeek expects, you cannot assign a value to FileAccess, although both of them are constants of the same base type.

 DayOfWeek day = FileAccess.Write; // doesn't work 

If you need this type of security and you donโ€™t need your enum to display any other type of behavior, then use the enum. If you are concerned that your enum is doing other things (e.g. enumeration, math operations, etc.), you should consider using classes. See my example below.

 public class LegalShipTypes { private readonly byte _numericValue; private readonly string _text; private LegalShipTypes(byte numericValue, string text) { _numericValue = numericValue; _text = text; } public byte Value { get { return _numericValue; } } public string Text { get { return _text; } } public static IEnumerable<LegalShipTypes> All { get { return new[] { Frigate, Cruiser, Destroyer, Submarine, AircraftCarrier }; } } public static readonly LegalShipTypes Frigate = new LegalShipTypes(1, "Frigate"); public static readonly LegalShipTypes Cruiser = new LegalShipTypes(2, "Cruiser"); public static readonly LegalShipTypes Destroyer = new LegalShipTypes(3, "Destroyer"); public static readonly LegalShipTypes Submarine = new LegalShipTypes(4, "Submarine"); public static readonly LegalShipTypes AircraftCarrier = new LegalShipTypes(5, "Aircraft Carrier"); } 

Now you can use it as a type:

 public class Fleet { private readonly List<LegalShipTypes> _ships; public Fleet() { _ships = new List<LegalShipTypes>(); } public LegalShipTypes Flagship { get; set; } public ICollection<LegalShipTypes> Ships { get { return _ships; } } } .... var fleet = new Fleet(); fleet.FlagShip = LegalShipTypes.AircraftCarrier; var iDoNotKnowWhyYouWouldNeedThisBut = LegalShipTypes.All.Sum(ship => ship.Value); Console.WriteLine("The flagship is a(n) \"{0}\".", fleet.FlagShip.Text); if (fleet.FlagShip == LegalShipTypes.AircraftCarrier) // this will work because it a reference comparison Console.WriteLine("This should be true"); 

As you can see, you still have type safety, but much more flexibility. This is more code, but you will not work against enumeration restrictions. To repeat, the listing should be simple. It should be easy. If your needs are simple, feel free to use them. If your needs are more complex, there is no shame in using old-fashioned object-oriented programming to solve your problem.

EDIT

In light of your last comment on the response that byte values โ€‹โ€‹represent the number of pegs, I strongly recommend that you not use enums to solve your problem. You would (ironically) try to put a circular anchor in a square hole.

+6
source

If you can edit the listing, and you need their sum in many places, you can put it in the listing:

 public enum LegalShipTypes : byte { Frigate = 1, Cruiser = 2, Destroyer = 3, Submarine = 4, AircraftCarrier = 5, All = Frigate + Cruiser + Destroyer + Submarine + AircraftCarrier } 

This makes sense in flag enumerations:

 [Flags] public enum LegalShipTypes : byte { Frigate = 1, Cruiser = 2, Destroyer = 4, Submarine = 8, AircraftCarrier = 16, All = Frigate | Cruiser | Destroyer | Submarine | AircraftCarrier } 

Or you can just use this:

 Enum.GetValues(typeof(LegalShipTypes)).Cast<byte>().Sum(x=>x) 

Which returns a decimal .

But this is a more general way to do this (it works regardless of the underlying enum type):

 public decimal Sum(Type enumType) { return Enum .GetValues(enumType) .Cast<object>() .Sum(x => (decimal)Convert.ChangeType(x, typeof(decimal))); } 
+8
source

Try the following, assuming this is an enumeration that inherits from System.Int32 (this is the default value).

 public int Sum(Type enumType) { return Enum.GetValues(enumType).Cast<int>().Sum(); } 

EDIT did not notice that the OP question is inherited from byte . Here is the version that works with byte

 public int Sum(Type enumType) { return Enum.GetValues(enumType).Cast<byte>().Select(x => (int)x).Sum(); } 
+5
source

See the accepted answer to this similar question:

How to list an enumeration?

You can get the values, list them, and simply sum the values โ€‹โ€‹in a for loop.

+1
source

Generic version of JaredPar's answer with Luke correction:

 public int Sum<T>(Type enumType) { return Enum.GetValues(enumType).Cast<T>().Sum(); } 

Call using:

 Sum<byte>(typeof(LegalShipTypes)); 

EDIT: Well, scratch this idea. I suppose:

 public int Sum(Type enumType) { return Enum.GetValues(enumType).Cast<byte>().Sum(); } 

- the answer.

0
source

If you want to summarize counter values, would you rather use a flag enumerator?

0
source
  public enum LegalShipTypes : byte { Frigate = 1, Cruiser = 2, Destroyer = 3, Submarine = 4, AircraftCarrier = 5 } class Program { static void Main() { byte sum = 0; foreach (byte item in Enum.GetValues(typeof(LegalShipTypes))) { sum += (byte)(object)item; } Console.WriteLine(sum); } } 
0
source

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


All Articles