IEnumberable.Max and Nullable types

Came across an interesting little snippet of code today.

This simply returns the maximum value of the “Value” property in the list. However there is a possible problem with this code, depending on the type of Value and the number of elements in the list.

If there are NO elements in the list and Value is NOT nullable (e.g. it is an Integer) then the above will throw an exception.

If there are NO elements in the list and Value IS nullable then it will return NULL.

If the list is empty and Value IS nullable then the above will also throw an exception.

If the list is empty and Value IS nullable then the above will return 0.

Getting the XmlEnumAttribute value for an Enum field

I recently had to convert an Enum back to its original Xml value so I thought I’d share the little method I wrote with you all incase you wondered how to do it yourself.

Method

Example Enum Class

Example usage

An Enum of TestEnumClass.FirstValue will then return “FirstValue”, while TestEnumClass.ThirdValue will return “The Third one”.

Hope you find that useful.