的Java到C#:扩展的通用 [英] Java to C#: Extends in Generic
问题描述
我想转换成该Java(安卓)code到C#(MonoDroid),但我不明白的<项目扩展OverlayItem>
公共类BalloonOverlayView<项目扩展OverlayItem>扩展的FrameLayout
它添加约束的类型参数。这是类似于在C#中,其中
条款。
在Java中,你有:
公共类BalloonOverlayView<项目扩展OverlayItem>扩展的FrameLayout
其中,项目
是必须继承或实现类型 OverlayItem
的类型参数。在C#,这将被写为:
公共类BalloonOverlayView<项目> :的FrameLayout,其中项目:OverlayItem
您可以看到如何约束被移动到了最后,但在其他类似的。这是非常常见的做法在C#中pfixed使用一个名称类型参数$ P $ 牛逼
,所以我会建议名称 TItem
像这样:
公共类BalloonOverlayView< TItem> :的FrameLayout其中,TItem:OverlayItem
这有助于使类型参数和普通类型之间明确的pretty的重要区别。
有关,当你想要用这样的,<一个类型约束的讨论href="http://stackoverflow.com/questions/4073852/why-use-generic-constraints-in-c-sharp/4073899#4073899">I进入这个详尽的previous答案。
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
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.
这篇关于的Java到C#:扩展的通用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!