简体   繁体   English

使用结构或类作为参数

[英]Using structs or classes as parameters

I'm fairly new to C# and programming in general, hence why I'm asking this question. 我一般对C#和编程都不熟悉,因此为什么要问这个问题。

I recently got my code reviewed by a colleague, and he stated that for the following method, I should use a class for some parameters. 最近,我的一位同事审查了我的代码,他说对于以下方法,我应该对某些参数使用一个类。

public List<Items> DoSomething(List<OtherItems> someItems, int amountOfItems, 
    DbGeopgrahpy Location)
{ //More stuff, irrelevant to the question.

He pointed out that I should put the amountOfItems and Location in a class which would contain those values instead. 他指出,我应该将amountOfItemsLocation放在包含这些值的类中。

This method will be called from a controller which returns this data to the client. 该方法将从控制器中调用,该控制器将这些数据返回给客户端。

Now my question is why ? 现在我的问题是为什么 I understand that it would improve readability (arguably imo), but is there any benefits to using classes as parameters? 我知道这会提高可读性(可以说是imo),但是将类用作参数有什么好处吗? I think I can assume this would even impact performance negatively when you have to declare a class each time you want to call this method. 我想我可以假设当您每次要调用此方法时都必须声明一个类时,这甚至会对性能产生负面影响。

I also found that structs can be used this way, if this is a good practice, when should I use structs and when should I use classes? 我还发现,如果这是一种好习惯,则可以以这种方式使用结构体,何时应该使用结构体以及何时应该使用类?

One of the benefit of using DTO class as method argument instead of collection of parameters is a preventing signature modification of method in future. 使用DTO类作为方法参数而不是参数集合的好处之一是将来可以防止方法的签名修改。 This may be useful for API public methods, for example, as allow you to modify API without breaking changes / method versioning. 例如,这对于API公共方法可能很有用,因为它允许您在不破坏更改/方法版本控制的情况下修改API。

Let's say you need to update/modify method and so you need to pass additional argument. 假设您需要更新/修改方法,因此需要传递其他参数。 If your method expect only one argument, that is class/struct, then you do not need to change the signature of method. 如果您的方法仅需要一个参数(即类/结构),则无需更改方法的签名。 Instead you just add additional property to your DTO class. 相反,您只需向DTO类添加其他属性。

It´s more a design awareness I think. 我认为这更多是一种设计意识。 If the method expects too many parameters that can be encapsulated in another object then that is spotting that you might haven´t done a good object definition. 如果该方法期望太多参数可以封装在另一个对象中,则说明您可能尚未完成良好的对象定义。 Also, as your software begin to grows you will find that it is much better to try to group everything in classes so you and the people that works with you will find much easier to mantain that way. 另外,随着软件的发展,您会发现最好将所有类归为一类,这样您和与您一起工作的人会发现维护起来更容易。

//avoid
public void Checkout(string shippingName, string shippingCity, 
       string shippingSate, string shippingZip, string billingName, 
       string billingCity, string billingSate, string billingZip)
{

}  

//DO
public void Checkout(ShippingAddress shippingAddress,BillingAddress billingAddress)
{
}

As you see, the common properties have been grouped to a common class that groups them. 如您所见,公共属性已分组为对它们进行分组的公共类。 You have to apply your criteria and do a good grouping. 您必须应用您的条件并进行良好的分组。

Personally, I find that if I have more than 3 or 4 parameters that are logically grouped (belong to the same sort of object or info), then I'll use a class. 我个人发现,如果我有3个或4个以上参数按逻辑分组(属于同一类对象或信息),那么我将使用一个类。

This is very arbitrary like you are finding. 就像您发现的那样,这是非常任意的。 Each person has their own set of rules as to when they want to use a class/struct. 每个人对于何时使用类/结构都有自己的一套规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM