对于函数的单个参数,在传递变量或整个对象时哪个更好 [英] Which will better in passing only variable or a whole object in case of single parameter for a function

查看:54
本文介绍了对于函数的单个参数,在传递变量或整个对象时哪个更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了返回一个List,进行四个不同的函数调用更好或者只有单个函数调用更好



ie

I想要一个List<>作为函数的返回类型。

例如列表与LT; T> GetData()



我使用四种不同的重载来获取List< t> as

GetData(int a)//这里只需要来自T的x作为返回

GetData(int a,int b)//这里只需要来自T的x和y作为返回

GetData(int a,string c)//这里只需要来自T的y作为返回

GetData(string a)//这里只需要来自T的z返回





我也可以这样做,



GetData(Type T)





哪个更好用?将特定变量作为参数或类型作为参数发送?为什么?

In order to return a List, making four different functions call is better or only single Function call is better

i.e.
I want a List<> as return type from a function.
e.g. List<t> GetData()

I am using four different overloading to get the List<t> as
GetData(int a) //here need only x from T as return
GetData(int a, int b) //here need only x and y from T as return
GetData(int a, string c) //here need only y from T as return
GetData(string a) //here need only z from T as return


Also I can do like,

GetData(Type T)


Which is better to use? Sending particular variable as parameter or Type as a parameter? Why?

推荐答案

这取决于你的目的,你的参数集是如何积分的。它还取决于重用此输入数据类型实例的典型程度(我的意思是,当您创建一个实例并在多次调用 GetData 中使用它时) 。



只有您的代码示例 GetData(Type T)与您的问题无关,因为名称类型建议 System.Type 不应该像那样使用。而不是类型,它应该是您的类或结构名称,比如说MyData。使用 struct ,它可能是 GetData(ref MyData data)(我甚至要解释原因吗?)...... < br $>


-SA
It just depends on your purpose, on how integral your set of parameters are. It also depends on how typical would be the reuse of the instance of this input data type (I mean, the cases when you create one instance and use it in several calls to GetData.

Only your code sample GetData(Type T) doesn't look relevant to your question, because the name "Type" suggests System.Type which should not be used like that. Instead of "Type", it should be your class or structure name, say "MyData". And with struct, it could be GetData(ref MyData data) (do I even have to explain why?)…

—SA


这篇关于对于函数的单个参数,在传递变量或整个对象时哪个更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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