VB.NET 中的类与模块 [英] Classes vs. Modules in VB.NET

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

问题描述

在 VB.NET 中使用模块而不是具有共享成员函数的类是否被认为是可接受的做法?

Is it considered an acceptable practice to use Modules instead of Classes with Shared member functions in VB.NET?

我倾向于避免使用模块,因为它们感觉像是 Visual Basic 6.0 的剩余部分,而且似乎不再适合.另一方面,使用只有共享成员的模块和类之间似乎没有太大区别.我并不经常需要它们,但有时它们会提供简单的解决方案.

I tend to avoid Modules, because they feel like leftover remains from Visual Basic 6.0 and don't really seem to fit in anymore. On the other hand, there doesn't seem to be much difference between using a Module and a Class with only Shared members. It's not that often that I really have much need for either, but sometimes there are situations where they present a simple solution.

我很想知道您是否有任何意见或偏好.

I'm curious to hear whether you have any opinion or preferences one way or the other.

推荐答案

Module 是 C# static 类的 VB 对应物.当您的类专为辅助函数和扩展方法而设计,并且您不想允许继承实例化时,您可以使用 模块.

Modules are VB counterparts to C# static classes. When your class is designed solely for helper functions and extension methods and you don't want to allow inheritance and instantiation, you use a Module.

顺便说一下,使用Module 并不是很主观,而且不推荐使用.实际上,您必须在适当的时候使用 Module..NET Framework 本身会多次执行此操作(例如,System.Linq.Enumerable).声明一个扩展方法,需要使用Modules.

By the way, using Module is not really subjective and it's not deprecated. Indeed you must use a Module when it's appropriate. .NET Framework itself does it many times (System.Linq.Enumerable, for instance). To declare an extension method, it's required to use Modules.

这篇关于VB.NET 中的类与模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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