[C#]我应该将方法的所有(或某些)参数包装到对象中吗? [英] [C#] should I wrap all(or some) params of a method into an object?

查看:72
本文介绍了[C#]我应该将方法的所有(或某些)参数包装到对象中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public int UpdateMC(Guid id
         , string description
         , decimal score
         , string optionA
         , string optionB
         , string optionC
         , string optionD
         , int optionAnswer
         , bool allowUploadAttachment = false
         , byte[] optionAImage = null
         , byte[] optionBImage = null
         , byte[] optionCImage = null
         , byte[] optionDImage = null
         , string optionAImageFileExtension = null
         , string optionBImageFileExtension = null
         , string optionCImageFileExtension = null
         , string optionDImageFileExtension = null
         , bool clearOptionAImage = false
         , bool clearOptionBImage = false
         , bool clearOptionCImage = false
         , bool clearOptionDImage = false
         , byte[] image = null
         , string imageFileExtension = null
         , bool clearImage = false
         , byte[] attachment = null
         , string attachmentFileExtension = null
         , bool clearAttachment = false){}





我有一些像上面这样的方法。参数是类的属性,但是这些方法将采用不同的组合进行处理(我的意思是,有些方法只会占用其中的一些,但有些方法几乎全部采用)。我的问题是:我应该将它们包装到对象上,然后将其传递给方法吗?因为如果我将它们分开,显然有点太长了。这两种方式传递参数有什么不同吗?哪个更高效或更标准?

谢谢。



我尝试了什么:



[此刻此处无需尝试。]



I have some methods like above. The parameters are properties of a class, but those methods will take different combo to process,(I mean, some methods will only take a few of them, but some will take almost all). My question is: should I wrap them into on object and then pass that to the method? Because it's obviously a bit too long if I seperate them. Is there any difference of theses two way to pass parameters? Which is more efficient or more standard?
thanks.

What I have tried:

[nothing needs to be tried here at the moment.]

推荐答案

对于那么多参数,我可能使用一个类 - 我可能会创建一个包含option,optionImage,optionImageFileExtension和clearOptionImage的类,并使用 params 来传递它们中的一些:

For that many parameters, I'd probably use a class - and I'd probably create one which held option, optionImage, optionImageFileExtension, and clearOptionImage, and use params to pass a number of them through:
public int UpdateMC(Guid Id, string description, decimal score, int optionAnswer, params MyClass[] options)



如果有些需要所有参数,我可能会创建一个类全部保留:


And if some need all of the parameters, I'd probably create a class that held them all:

public class MyParameterClass
   {
         public string description {get; set;}
         public decimal score {get; set;}
         public int optionAnswer {get; set;}
         public bool allowUploadAttachment {get; set;}
         public byte[] image{get; set;}
         public string imageFileExtension {get; set;}
         public bool clearImage {get; set;}
         public byte[] attachment {get; set;}
         public string attachmentFileExtension {get; set;}
         public bool clearAttachment {get; set;}
         public List<MyClass> options {get; set;}
   }


这篇关于[C#]我应该将方法的所有(或某些)参数包装到对象中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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