使用列表<> generic作为方法返回类型 [英] Using a List<> generic as a method return type

查看:50
本文介绍了使用列表<> generic作为方法返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的代码示例比尝试描述问题更容易。我
有:


公共类myClassA

{

解决方案

pa*********@abbott.com 写道:


一个简单的代码示例比尝试描述问题更容易。我
有:


公共类myClassA

{







}


公共类myClassB {





public List< myClassAclassBfunc()

{







}





}


当我尝试编译时,我得到错误}预期在线上

包含classBfunc()的开头大括号。我在做什么

错了?我正在尝试的是什么?



我得到一个不同的错误:类,结构或接口中的无效标记'。'' >
会员声明" ;.


如果您想要确认编译错误,请发布完整的示例,

否则我们无法分辨实际错误可能在哪里。

您想要做的是什么,并且您正在使用正确的语法,所以它是

任何人的猜测。


-

J.


public List< myClassAclassBfunc< myClassA>()< br $>
{}


通用参数需要传递给函数

< pa ******** *@abbott.com写信息

新闻:2d ******************************** ** @ c65g2000 hsa.googlegroups.com ...


>一个简单的代码示例比尝试描述问题更容易。我
有:


公共类myClassA

{







}


公共类myClassB {





public List< myClassAclassBfunc()

{







}





}


当我尝试编译时,我得到错误}预期在线上

包含classBfunc()的开头大括号。我在做什么

错了?我正在尝试的是什么?


TIA


Bill McCarthy写道:
< blockquote class =post_quotes>
public List< myClassAclassBfunc< myClassA>()

{}


泛型参数需要传递给功能



好​​抓!这是区别


公共列表< myClassAclassBfunc()





公开列表< ; myClassAclassBfunc< myClassA>()

前者是一种非泛型方法,它返回类型为myClassA的List的特定实例化

。后者是一种通用方法,

使用类型参数myClassA返回List的实例化。


如果myClassA没有定义,第一个是错误。因此,如果myClassA是

a混凝土类型,OP'可能缺少装配参考。


-

J.

A simple code example is easier than trying to describe the issue. I
have:

public class myClassA
{

解决方案

pa*********@abbott.com wrote:

A simple code example is easier than trying to describe the issue. I
have:

public class myClassA
{
.
.
.
}

public class myClassB {
.
.
public List<myClassAclassBfunc()
{
.
.
.
}
.
.
}

When I attempt to compile, I get the error "} expected" on the line
containing the opening curly brace of classBfunc(). What am I doing
wrong? Is what I''m trying possible?

I get a different error: "Invalid token ''.'' in class, struct, or interface
member declaration".

Please post complete examples if you want compile errors diagnosed,
otherwise it''s impossible for us to tell where the actual error might be.
What you want to do is possible and you''re using the correct syntax, so it''s
anyone''s guess.

--
J.


public List<myClassAclassBfunc<myClassA>()
{}

The generic parameter needs to be passed to the function
<pa*********@abbott.comwrote in message
news:2d**********************************@c65g2000 hsa.googlegroups.com...

>A simple code example is easier than trying to describe the issue. I
have:

public class myClassA
{
.
.
.
}

public class myClassB {
.
.
public List<myClassAclassBfunc()
{
.
.
.
}
.
.
}

When I attempt to compile, I get the error "} expected" on the line
containing the opening curly brace of classBfunc(). What am I doing
wrong? Is what I''m trying possible?

TIA


Bill McCarthy wrote:

public List<myClassAclassBfunc<myClassA>()
{}

The generic parameter needs to be passed to the function

Good catch! This is the difference between

public List<myClassAclassBfunc()

with

public List<myClassAclassBfunc<myClassA>()

The former is a non-generic method that returns a particular instantiation
of List with the type "myClassA". The latter is a generic method that
returns an instantiation of List using the type parameter "myClassA".

If "myClassA" is not defined, the first one is an error. So if "myClassA" is
a concrete type, the OP''s probably missing an assembly reference.

--
J.


这篇关于使用列表&lt;&gt; generic作为方法返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆