函数重载与 VB.NET 中的默认参数? [英] Function overloading vs. default parameters in VB.NET?

查看:43
本文介绍了函数重载与 VB.NET 中的默认参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.NET中,哪个更好用:函数重载还是默认参数?

In VB.NET, which is better to use: function overloading or default parameters?

推荐答案

代码会被其他语言使用吗?如果是这样,那将平衡转向过载,同时仍然牢记 Hamish 的回答.特别是,C# 不支持可选参数 - 但...

Is the code going to be used by other languages? If so, that swings the balance towards overloads while still bearing Hamish's answer in mind. In particular, C# doesn't support optional parameters - yet...

诚然,这实际上并不会阻止某人使用您的 C# 代码,只是对他们来说可能会很痛苦.

Admittedly this wouldn't actually prevent someone using your code from C#, it just might be a pain for them.

如果有很多参数并且它们在逻辑上代表某些东西,您可能需要考虑将它们封装在一起,就像 ProcessProcessStartInfo 一起工作一样.由于对象初始值设定项,这在 C# 中特别好.

If there are a lot of parameters and they logically represent something, you might want to consider encapsulating them together, in the same way that Process works with ProcessStartInfo. That's particularly nice from C# due to object initializers.

如果这是用于构造,您也可以将构建器模式视为此的变体.例如,在协议缓冲区中,我可以执行以下操作:

If this is for construction, you might also consider the builder pattern as a variant of this. For instance, in Protocol Buffers I can do something like:

Person jon = new Person.Builder { Name="Jon", Age=32,
                                  Spouse="Holly", Kids=3 }.Build();

最终变得非常易读,同时仍然可以一次性创建一个人(在一个表达式中,而不必改变人本身——实际上消息类型是不可变的;只有构建器不可变).

which ends up being very readable while still creating a person in one go (in one expression, and without having to mutate the person itself - indeed the message type is immutable; it's only the builder which isn't).

这篇关于函数重载与 VB.NET 中的默认参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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