I have the following enum:
public enum LegalShipTypes : byte
{
Frigate = 1,
Cruiser = 2,
Destroyer = 3,
Submarine = 4,
AircraftCarrier = 5
}
Is there is a way to get the total value of enum in any way? For instance, this would result in (1 + 2 + 3 + 4 + 5) = 15.
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 ...
Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.
The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
If you can edit the enum, and you need their sum in many places, you can put it in the enum itself:
public enum LegalShipTypes : byte {
Frigate = 1,
Cruiser = 2,
Destroyer = 3,
Submarine = 4,
AircraftCarrier = 5,
All = Frigate + Cruiser + Destroyer + Submarine + AircraftCarrier
}
This makes more sense in flags enums though:
[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 it (works regardless of the underlying type of the enum):
public decimal Sum(Type enumType) {
return Enum
.GetValues(enumType)
.Cast<object>()
.Sum(x => (decimal)Convert.ChangeType(x, typeof(decimal)));
}
I didn't want to type this up as an answer, because it doesn't answer your question directly, but based on your comment in response to my comment to your question, it merits some explanation.
Enums are meant to be very simple type safe representations of state. If you simply use constants, then you can assign the wrong constants to a value. This prevents you from assigning the wrong type of constant to a field. For example, if you have something that expects DayOfWeek, you can't assign a FileAccess value, even though they are both constants of the same underlying type.
DayOfWeek day = FileAccess.Write; // doesn't work
If you need this type safety and you don't need for your enum to exhibit any other type of behavior, then use an enum. If you are concerned with having your enum do other things as well (such as enumeration, mathematical operations, etc) then 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 in a typesafe way like this:
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's a reference comparison
Console.WriteLine("This should be true");
As you can see, you still have type safety, but much more flexibility. It is more code, but you won't find yourself working against the limitations of enum. To reiterate, enum is meant to be simple. It's supposed to be simple. If your needs are simple, don't hesitate to use it. If your needs are more complex, there's no shame in using good old fashioned object oriented programming to solve your problem.
EDIT
In light of your last comment response that the byte values represents the number of pegs, I would highly recommend you don't use enums to solve your problem. You'd be (ironically) trying to put a round peg in a square hole.
Try the following assuming it's an enum that inherits from System.Int32 (this is the default).
public int Sum(Type enumType) {
return Enum.GetValues(enumType).Cast<int>().Sum();
}
EDIT didn't notice the OP's question inherits from byte
. Here's a version that works with byte
public int Sum(Type enumType) {
return Enum.GetValues(enumType).Cast<byte>().Select(x => (int)x).Sum();
}
See the accepted answer of this similar question:
How do I enumerate an enum?
You could get the values, enumerate through them and just sum the values in the for loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With