When to use generic methods and when to use wild-card?

前端 未结 9 2296
猫巷女王i
猫巷女王i 2020-11-22 10:32

I am reading about generic methods from OracleDocGenericMethod. I am pretty confused about the comparison when it says when to use wild-card and when to use generic methods.

9条回答
  •  温柔的废话
    2020-11-22 10:55

    Wildcard method is also generic - you could call it with some range of types.

    The syntax defines a type variable name. If a type variable has any use (e.g. in method implementation or as a constraint for other type), then it makes sense to name it, otherwise you could use ?, as anonymous variable. So, looks like just a short-cut.

    Moreover, the ? syntax is not avoidable when you declare a field:

    class NumberContainer
    {
     Set numbers;
    }
    

提交回复
热议问题