C#中的类和模块是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2168495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:05:42  来源:igfitidea点击:

What are classes and modules for in C#

c#classmodule

提问by Arlen Beiler

Can someone explain the difference between a class and a module. When do you use one versus the other? I am using C#.

有人可以解释类和模块之间的区别吗?你什么时候使用一个和另一个?我正在使用 C#。

Update: I do mean the C# equivalent of the VB Module.

更新:我的意思是 C# 相当于 VB 模块。

采纳答案by Vivek Bernard

(Maybe I should be clear, there are no "Modules" in C# as in VB.NET's "Module")

(也许我应该清楚,C# 中没有像 VB.NET 的“模块”中那样的“模块”)

There are no modules in C# (like the modules in VB or VB.NET). So, instead a module is one which is compiled and packaged into an assembly, it's more logical.

C# 中没有模块(就像 VB 或 VB.NET 中的模块)。因此,模块是一个被编译并打包成程序集的模块,它更合乎逻辑。

Whereas class is a well defined entity. A module may use a class (or classes), to function. (Again the word "module" is used logically)

而类是一个定义明确的实体。一个模块可以使用一个类(或多个类)来运行。(同样,“模块”这个词是合乎逻辑的)

The word "Module" is also used in a context quite different, in System.Reflection.Module

在 System.Reflection.Module 中,“Module”一词也用在完全不同的上下文中

回答by Nick Craver

A module is a compiled dll or exe, it contains the compiled classes. A class is the same as a class in most any other language.

模块是编译后的 dll 或 exe,它包含编译后的类。类与大多数其他语言中的类相同。

Also, modules, whether it's one or more are what make up Assemblies in .Net

此外,模块,无论是一个还是多个,都是 .Net 中的程序集的组成部分

Remember that once it's compiled in .Net it doesn't matter what language it was written in, it's all IL, so the terms you're describing are pretty much language agnostic at that point.

请记住,一旦它在 .Net 中编译,它是用什么语言编写的并不重要,它都是 IL,所以你所描述的术语在那时几乎是语言不可知的。

回答by Aaronaught

A classis an independent unit of data and functions - fields, properties, and methods.

是数据和功能的独立单元-字段,属性和方法。

A modulerefers to the code generated from a single physical file. Most of the time, when you write code in Visual Studio, a single file will contain a single class, interface, or struct.

模块指的是从一个单一的物理文件生成的代码。大多数情况下,当您在 Visual Studio 中编写代码时,单个文件将包含单个类、接口或结构。

Compiled DLLs or EXEs are called assemblies. An assembly can contain any number of modules as well as other resources such as version information. By default, when you compile an assembly in Visual Studio, the assembly contains every module defined in the project.

编译后的 DLL 或 EXE 称为程序集。程序集可以包含任意数量的模块以及其他资源,例如版本信息。默认情况下,在 Visual Studio 中编译程序集时,程序集包含项目中定义的每个模块。

As for when you would use them: You use classes, interfaces and structs all the time, those are the basics. Modules are things that you generally don't have much control over; again, it's a generally accepted practice to have one type per file which compiles to one type per module.

至于何时使用它们:您一直在使用类、接口和结构,这些都是基础知识。模块是您通常无法控制的东西;同样,每个文件有一种类型,每个模块编译为一种类型是一种普遍接受的做法。

You would normally use a separate assembly when you want to reuse certain types (contained within certain modules) across multiple projects. You might also use it to create a hard boundary between different components of an application, i.e. put your database logic in one assembly and your business logic in another.

当您想要在多个项目中重用某些类型(包含在某些模块中)时,您通常会使用单独的程序集。您还可以使用它在应用程序的不同组件之间创建硬边界,即将数据库逻辑放在一个程序集中,将业务逻辑放在另一个程序集中。

回答by Kb.

.NET Framework moduleis not the same as a modulein Visual Basic, which is used by a programmers to organize functions and subroutines in an application ( see module class in C#)
If you are used to the VB6 module (method that is accessible wihout an instance), declare the functions as staticin c#

.NET框架组件是不一样的一个模块在Visual Basic中,用于由程序员来组织应用程序中的功能和子程序(见在C#中的模块类
如果你使用VB6的模块(方法是访问wihout一个实例),在 c#中将函数声明为静态

回答by John Saunders

There's no equivalent in C# to the VB6 module. The closest would be a static class, but even then, the members of the class are not global, as they would be in a VB6 module.

在 C# 中没有与 VB6 模块等效的东西。最接近的是静态类,但即便如此,类的成员也不是全局的,因为它们在 VB6 模块中。

回答by JaredPar

This depends heavily on which "Module" you are referring to.

这在很大程度上取决于您指的是哪个“模块”。

Visual Basic's Module

Visual Basic 的模块

There is no real equivalent in C# for a VB.Net Module. In ways it's similar to a static class in that you cannot create an instance of it and all of the members inside of it are static. In C# this requires an explicit static keyword while in VB.Net it's implicit.

VB.Net 模块在 C# 中没有真正的等价物。在某种程度上,它类似于静态类,因为您无法创建它的实例,并且其中的所有成员都是静态的。在 C# 中,这需要一个显式的静态关键字,而在 VB.Net 中它是隐式的。

The big difference though is in name lookup. In VB.Net if a Module is in scope then any of it's methods can be called without qualification. This is not true for C# static classes.

但最大的区别在于名称查找。在 VB.Net 中,如果模块在范围内,则可以无条件调用它的任何方法。这不适用于 C# 静态类。

Besides name lookup, the primary difference between a class and a module is that a class is typically useful only with an instance of the type.

除了名称查找之外,类和模块之间的主要区别在于类通常仅对类型的实例有用。

System.Reflection.Module

系统.反射.模块

A module in this context is a portable executable file (dll or exe). A given DLL/EXE can be composed of several modules if it's a multi-file assembly. It has no real relationship to an individual class.

此上下文中的模块是可移植的可执行文件(dll 或 exe)。如果一个给定的 DLL/EXE 是一个多文件程序集,它可以由多个模块组成。它与单个类没有真正的关系。

回答by Ulysses Alves

From Microsoft docs,

微软文档

A module is a portable executable file, such as type.dll or application.exe, consisting of one or more classes and interfaces. There may be multiple namespaces contained in a single module, and a namespace may span multiple modules.

One or more modules deployed as a unit compose an assembly. For information about creating an assembly with more than one module, see Multifile Assemblies.

Note that a .NET Framework module is not the same as a module in Visual Basic, which is used by a programmers to organize functions and subroutines in an application.

模块是一个可移植的可执行文件,例如 type.dll 或 application.exe,由一个或多个类和接口组成。一个模块中可能包含多个命名空间,一个命名空间可能跨越多个模块。

作为一个单元部署的一个或多个模块组成一个程序集。有关使用多个模块生成装配信息,请参见多文件组件

请注意,.NET Framework 模块与 Visual Basic 中的模块不同,后者被程序员用来组织应用程序中的函数和子例程。