Exercises in this lecture   previous -- Keyboard shortcut: 'p'        Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home      

Exercise 5.6
Extending struct Double

At the accompanying page we have seen how class Point can be extended with the instance method DistanceTo. This is an extension method.

If you study the method DistanceTo you will see that we use the squareroot function Math.Sqrt, defined statically in class Math. And we could/should have used a similar Square function had it been available.

It is remarkable that C# 3.0 allows us to extend the structs behind the primitive types, such as Double and Int32.

Write a static class that extends struct Double with the two extension methods Sqrt and Square. Afterwards, rewrite the method DistanceTo such that it makes use of the new instance methods in struct Double.