I 开发者_JS百科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.
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 want to be summing enumerator values, wouldn't you be better off using a flagged enumerator?
A generic version of JaredPar's answer with Luke's correction:
public int Sum<T>(Type enumType) {
return Enum.GetValues(enumType).Cast<T>().Sum();
}
Call with:
Sum<byte>(typeof(LegalShipTypes));
EDIT: Well, scratch that idea. I suppose:
public int Sum(Type enumType) {
return Enum.GetValues(enumType).Cast<byte>().Sum();
}
is the answer.
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);
}
}
精彩评论