如何将许多参数传递给方法? [英] How to pass many parameters to a method?

查看:84
本文介绍了如何将许多参数传递给方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将16个参数传递给一个方法,但我不想在括号中一一列出(如果我一一列出,我觉得代码太丑了.)想知道哪种方法可以解决将许多参数传递给方法的问题.谢谢.

I need to pass 16 parameters to a method,but I don''t want to list them in parentheses one by one(if I list them one by one ,I feel the code is so ugly.).So,I want to know which method can solvle passing many parameters to a method. Thank you.

推荐答案

我建​​议传递一个class(object)的实例,这将对您有所帮助,并且省去了在目标位置进行类型转换的工作(方法)

I would recommend pass an instance of the class(object) and this would help you by all means and also eliminates the effort of type casting at destination(the method)

public class MyMethodParams
{  public int ID { get; set; }
   public string Name { get; set; }
   public double Height { get; set; }
   public List<string> Likes { get; set; }
   public MyMethodParams(){}

...
...
}




让您的方法接受MyMethodParams实例作为参数:




Let your method accepts MyMethodParams instance as paramter:

public object MyMethod(MyMethodParams myparams)
{
int ID = myparams.ID;
string Name = myparams.Name; 
double Height = myparams.Height;
...
}



不只是创建一个实例并调用您的方法.



No just create an instance and call your method.

MyMethodParams myparInst = new MyMethodParams();
myparInst.ID = 1;
myparInst.Name = "George";
myparInst.Height = 2.03d;
....
.....

object obj = MyMethod(myparInst);


向一个方法传递6个以上的参数很丑陋,而且会产生反作用,一个更好的选择是创建一个参数对象并将其作为单个参数传递,这使您可以提供所需的参数,而不必记住顺序等.

Passing more than 6 parameters to a method is ugly and counter productive, a better option is to create a parameter object and pass that as a single parameter, this allows you to supply the parameters needed and not remember the sequence etc.

public class myparams
{
  public string param1 = "default"; // defaults
  public int paramName2 = 20;
  public DateTime date = DateTime.Now;
}

public void Method(myparams par)
{
   // your code here
}


// calling
Method(new myparams  { paramName2 = 100, param1="hello"}); // everything else is default and you get intellisence for completion and ordering doesn't matter


有几种方法可以将信息传递给方法.例如:
-您可以列出所有参数,但如果可以在方法中使用默认值,则可以选择其中的一些参数
-您可以使用参数 [列表 [ ^ ]包含参数
-当然,您可以创建一个类,其中包含您需要传输的信息.

根据情况,使用强类型参数可能是明智的.如果适用,那么使用可选参数或单独的类保存数据很可能是最佳选择.如果您不需要/不需要强类型化的参数,那么params可能是一个不错的选择.
There are several ways to pass information to a method. For example:
- you can list all the parameters, but make some of the optional if you can have a default value in the method
- you can use params[^] to define variable amount of parameters
- you can use for example a list[^] to contain the parameters
- and of course you can create a class that holds the information you need to transfer.

Depending on the situation, it may be wise to use strongly typed parameters. If that is applicable, then using optional parameters or a separate class to hold the data are most likely the best choices. If you don''t need/want strongly typed parameters, then params could be a good choice.


这篇关于如何将许多参数传递给方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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