3

I am trying to convert this Java (Android) code to c# (MonoDroid) but I don't understand the <Item extends OverlayItem>

public class BalloonOverlayView<Item extends OverlayItem> extends FrameLayout
Ian Vink
  • 66,960
  • 104
  • 341
  • 555

3 Answers3

9

It's adding a constraint to the type parameter. It's analogous to the where clause in C#.

In Java, you have:

public class BalloonOverlayView<Item extends OverlayItem> extends FrameLayout

Where Item is a type parameter that must subclass or implement type OverlayItem. In C# this would be written as:

public class BalloonOverlayView<Item> : FrameLayout where Item : OverlayItem

You can see how the constraint is moved to the end, but otherwise analogous. It is very much common practice in C# to name type parameters prefixed with a T, so I would recommend the name TItem like so:

public class BalloonOverlayView<TItem> : FrameLayout where TItem : OverlayItem

This helps make clear the pretty important distinction between type parameters and ordinary types.

For a discussion on when you'd want to use type constraints like this, I go into this at length in a previous answer.

Community
  • 1
  • 1
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
3

It is the same as this:

public class BalloonOverlayView<Item> : FrameLayout where Item : OverlayItem
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
1

This means that the parametrised type Item has to be a subclass of OverlayItem

Semantically, this implies that it makes to no sense to instantiate BalloonOverlayView with a parameterised type if it does not extend OverlayItem

Rob Kielty
  • 7,958
  • 8
  • 39
  • 51