5 Answers
5

First off you're throwing an exception, followed by a return - the return statement will never be hit as the exception will stop execution of the rest of the method, making the return statement superfluous.

Secondly, you can't return null when the return type is float; you'd have to change the return type to be float? (see: nullable types)

So either, if it is a real error case, as there is nothing you can do go with just the exception:

Personally, if RawData being null is an error condition / exceptional circumstance that should never happen then I would say throw the exception, and handle the exception if thrown in the calling code.

An alternative approach would be to force initialisation of RawData through the constructor, make RawData private (or at least the setter) and throwing the exception there. Leaving any other logic within the class clean of any exception throwing / null-checking as it can assume that RawData will have been set previously.

The return statement wouldn't compile because float is a value type which can never be null unless you use the nullable type float?

Personally from the code sample you've given I would throw the exception as you currently expose the RawData object via a public setter so you have no guarantee that it won't be null when GetMaxX is called. The exception can then be propagated up the stack and be caught at any level, whereas by making the return type nullable you would have to add additional checks to the calling code to see whether your method had returned null and handle that appropriately.

You can remove the return statement that is below the throw exception statement. Whenever an exception is thrown, no other statement is executed in the method (finally blocks are an exception to this, but irrevelant in this context).

Except this problem about the function, I have an argument about the RawData property being accessible publicly. It is not generally a good idea to open a collection like that. As @sq33G suggests, you can guarantee to have a valid object by passing RawData as a constructor parameter. And you can fail early in the constructor when an invalid array is passed (null, maybe of size zero?).

If it is neccessary that RawSata be accessible from outside of the class, I suggest you to do it in a way that neither the array itself (so no setter) nor its content can be altered. Using an IEnumerable is a proper way of doing it.