如何在C#中将ienumerable列表作为参数传递 [英] How to pass ienumerable list as parameter in C#

查看:639
本文介绍了如何在C#中将ienumerable列表作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我有候选人,证书和考试作为班级。



和我将这些列入名单,IEnumerable< candidate>候选人名单,

IEnumerable< ceritificates>证书列表和IEnumerable< examsessions> ExamSession列表。



我尝试过:



现在,我的问题是:

我写了一个方法,其中,我想通过IEnumerable<>作为参数。在这里,我不会调用哪个对象(Candidate / Certificate / ExamSession)。

任何人都可以帮助我,如何在给定的方法中传递IEnumerable作为参数...


谢谢

解决方案

不清楚你要求的是什么,但这里有两个例子......



1.标准< pre lang =c#> void FuntionName(IEnummerable< candidate> Data)
{
/ / 处理数据...
}

2。 Generic

  void  FuntionName< T>(IEnummerable< T> Data)
{
// 执行数据处理...
}

非常简单 - 编程101 ...


除非这三个类共享一个基类,否则不能将它们的集合传递给方法,除非作为<$ c的集合$ c> object 实例,除了你可以编写一个方法来接受三个类中任何一个的单个实例,除非有从A到B的直接转换,反之亦然。 />


所以你可以传递一组对象:

 List< MyClass> obs =  new  List< MyClass>(); 
...
MyMethod(obs);
...
public void MyMethod(IEnumerable< object> collection)
{
...
}

但这是一个非常糟糕的设计。

并且它不太可能有充分的理由你提到的三个类共享一个共同的基础,所以编写单个方法来获取所有三个类的集合不太可能有一个好的设计理由,因为它首先必须做的是找出它已被传递,因此它可以适当地投射对象以使用它们。

更好 - 但不是很多 - 的方法是创建四个重载:

 public void MyMethod(IEnumerable< Candidate> c){MyMethod(c,null,null); } 
public void MyMethod(IEnumerable< Certificates> c){MyMethod(null,c,null); }
public void MyMethod(IEnumerable< ExamSession> c){MyMethod(null,null,c); }
public void MyMethod(IEnumerable< Candidate> cand,IEnumerable< Certificates> cert,IEnumerable< ExamSession> es)
{
...
}

至少就这样,你可以更容易地检查,并保留一些强有力的打字。



但说实话,你最好重新考虑这种方法


Dear All,

I have Candidate, Certificates and ExamSession as class.

And i'm getting these in a list, IEnumerable<candidate> for list of candidates,
IEnumerable<ceritificates> for list of certificates and IEnumerable<examsessions> for list of ExamSession.

What I have tried:

Now, my question is:

I wrote a method, in which, i want to pass IEnumerable<> as a Parameter. Here I don't which object I will call(Candidate/Certificate/ExamSession).

Can any one please help me, how to pass IEnumerable as a parameter in a given method...


Thanks

解决方案

Unclear what you are asking for but here are 2 examples...

1. Standard

void FuntionName(IEnummerable<candidate> Data)
{
    // do stuff with data...
}

2. Generic

void FuntionName<T>(IEnummerable<T> Data)
{
    // do stuff with data...
}

Pretty simple - programming 101...


Unless the three classes share a base class, you can't pass a collection of them to a method, except as a collection of object instances, any more than you could write a method that would take a single instance of any of the three classes, unless there is a direct conversion from A to B and vice versa.

So you could pass a collections of objects:

List<MyClass> obs = new List<MyClass>();
...
MyMethod(obs);
...
public void MyMethod(IEnumerable<object> collection)
   {
   ...
   }

But that's a very poor design.
And it's unlikely that there will be a good reason for the three classes you mention to share a common base, so it's not likely that there would be a good design reason to wrote a single method to take a collection of all three classes, as pretty much the first thing it would have to do is find out what it had been passed so it could cast the objects appropriately to use them.
A better - but not much - approach would be to create four overloads:

public void MyMethod(IEnumerable<Candidate> c) { MyMethod(c, null, null); }
public void MyMethod(IEnumerable<Certificates> c) { MyMethod(null, c, null); }
public void MyMethod(IEnumerable<ExamSession> c) { MyMethod(null, null, c); }
public void MyMethod(IEnumerable<Candidate> cand, IEnumerable<Certificates> cert, IEnumerable<ExamSession> es)
    {
    ...
    }

As at least that way you can check more easily, and retain some strong typing.

But to be honest you would be better off rethinking this approach.


这篇关于如何在C#中将ienumerable列表作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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