用于返回对象列表的通用接口 [英] generic interface to return list of objects

查看:63
本文介绍了用于返回对象列表的通用接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建界面以返回事物或对象列表的最佳方法是什么?

例如:



< pre lang =c#> public interface MyInterface
{
List< object> ; ComputeSomething( string 输入)
}





当然是以上是对的吗?



谢谢!

解决方案

这两个选项中的一个怎么样?



  public   interface  MyInterface 
{
List< T> ComputeSomething< T>( string 输入);
}

public interface MyInterface< T>
{
List< T> ComputeSomething( string 输入);
}


缺少是方法声明结束时的字符';'。没有它,你的代码将无法编译。



至于目的......我怀疑有没有。没有目的,任何人都无法说出是对还是错。出于一个目的而对另一个目的来说可能是错误的。



在这种情况下,我怀疑它是否有任何意义,因为系统。对象是所有类型的基本类型,因此您的列表基本上是无类型的。至少它会导致OOP的基本违规。也许你必须学习动态调度和多态性,即OOP的基础知识。更可疑的是从某些字符串中生成一些列表。它强烈建议您倾向于使用表示数据的字符串而不是使用数据本身。



如果您对该主题还有更多疑问,欢迎您问他们,但我们只有在你分享你的最终目标时才能讨论其他事情。







另请注意,您的界面不是通用的。例如,通用接口的示例将是:b $ b

  interface  MyInterface< ARGUMENT,RETURN> 
{
RETURN SomeFunction(ARGUMENT arg);
void SomeMethod();
void SomeOtherMethod(ARGUMENT arg);
// ...
}

< br $>


-SA


what is the best way of creating an interface to return a list of 'things' or objects
for example:

public interface MyInterface
{
   List<object> ComputeSomething(string input)
}



Of course the above is not correct right?

thanks!

解决方案

How about one of these two options?

public interface MyInterface
{
    List<T> ComputeSomething<T>(string input);
}

public interface MyInterface<T>
{
    List<T> ComputeSomething(string input);
}


Missing is the character ';' at the end of method declaration. Without it, your code won't compile.

As to the purpose of that… I doubt there is any. And without the purpose, no one can say if something is right or wrong. It can be wrong for one purpose and right for another one.

In this case, I doubt it can make any sense, because System.Object is the base type of all types, so your list is essentially untyped. At least it will lead to the very basic violation of OOP. Perhaps you have to learn dynamic dispatch and polymprphism, that is, the basics of OOP. Even more suspicious is generation of some list out of some string. It pretty strongly suggests that you tend to work with strings representing data instead of using data itself.

If you still have more questions on the topic, you are very welcome to ask them, but we can discuss something else only if you share your ultimate goals.

[EDIT]

Also note that your interface is not generic. An example of generic interface would be, for example,

interface MyInterface<ARGUMENT, RETURN>
{
    RETURN SomeFunction(ARGUMENT arg);
    void SomeMethod();
    void SomeOtherMethod(ARGUMENT arg);
    // ...
}



—SA


这篇关于用于返回对象列表的通用接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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