我如何返回的IEnumerable< T>从方法 [英] How do i return IEnumerable<T> from a method

查看:216
本文介绍了我如何返回的IEnumerable< T>从方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发接口的示例项目,我希望它能够尽可能地通用,所以我创造了像一个界面下方

 公共接口IUserFactory 
{
IEnumerable的<用户>得到所有();
用户GetOne(INT ID);
}



但随后发生的事情,我不得不重复做界面下方

 公共接口IProjectFactory 
{
IEnumerable的<项目和GT; GETALL(用户用户);
项目GetOne(INT ID);
}

如果你看了上面的区别就是他们返回类型,所以我创建的东西像下面才发现我得到错误不能解析符号为T 我究竟做错了

 公共接口IFactory的
{
IEnumerable的< T>得到所有(); $ B $(B T)GetOne(INT ID);
}


解决方案

您需要使用一个通用的接口 /的class ,不只是泛型方法:在

 公共接口IFactory的< T> 
{
IEnumerable的< T>得到所有(); $ B $(B T)GetOne(INT ID);
}



定义接口的泛型类型/类确保类型是整个知类(无论类型说明符)。


I am developing Interface for a sample project i wanted it to be as generic as possible so i created a interface like below

public interface IUserFactory
{    
    IEnumerable<Users> GetAll();
    Users GetOne(int Id);
}

but then it happened i had to duplicate the interface to do below

public interface IProjectFactory
{    
    IEnumerable<Projects> GetAll(User user);
    Project GetOne(int Id);
}

if you looked at above difference is just types they return, so i created something like below only to find i get error Cannot Resolve Symbol T What am i doing wrong

public interface IFactory
{    
    IEnumerable<T> GetAll();
    T GetOne(int Id);
}

解决方案

You need to use a generic interface/class, not just generic methods:

public interface IFactory<T>
{    
    IEnumerable<T> GetAll();
    T GetOne(int Id);
}

Defining a generic type on the interface/class ensures the type is known throughout the class (wherever the type specifier is used).

这篇关于我如何返回的IEnumerable&LT; T&GT;从方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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