1

I'm trying to make a method that returns the highest value of an Array with generic elements that can be Nullable.

 public T Greatest<T>(T?[] array) where T : struct, IComparable<T> { T? Greater = null; foreach (var elem in array) { if(elem.HasValue) { if(Greater.HasValue) { if(Greater.Value.CompareTo(elem.Value) < 0) { Greater = elem; } } else { Greater = elem; } } } //Problem here: What is the best way to return the greatest value? // **** return Greater.Value; // Possible InvalidOperationException // **** } 

What is the proper way to return the value?

1
  • 1
    Since you`re just enumerating the input, you might consider passing in an IEnumerable<T> rather than the array. Commented Jun 27, 2011 at 17:27

5 Answers 5

4

Nullable.GetValueOrDefault

Sign up to request clarification or add additional context in comments.

Comments

4

You can simplify your method:

public T Greatest<T>(T?[] array) where T : struct, IComparable<T> { return array.Max().GetValueOrDefault(); } 

Comments

3

You can simply change the method to return T? and then return Greater. If the greatest value isn't a value at all, why pretend that it is? On the other hand, if you really need to return some kind of non-nullable value, then you can do

if (Greater.HasValue) return Greater.Value; else return /* define your default here */ 

However, defining a default that is appropriate for all types T would be difficult, so you can also rely on the automatic default of the type.

return Greater.GetValueOrDefault(); // returns either Value or default(T) 

Which is the functional equivalent of

if (Greater.HasValue) return Greater.Value; else return default(T); 

Comments

0

To get your method to compile, you could pull the default.

return (Greater.HasValue) ? Greater.Value : default(T); 

That said, if you're using a modern version of C#, you should not be writing this method in the first place and should instead be using Enumerable.Max().

Comments

0

OP said

What is the properly [sic] way to return the Value?

The proper way is to not reinvent the wheel:

public T Greatest<T>( IEnumerable<T?> list ) where T : struct, IComparable<T> { return list.Where( x => x.HasValue ) .Select( x => x.Value ) .Max() ; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.