What are classes and modules for in C#

2019-06-14 22:27发布

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

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

标签: c# class module
6条回答
欢心
2楼-- · 2019-06-14 22:53

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.

Also, modules, whether it's one or more are what make up Assemblies in .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.

查看更多
在下西门庆
3楼-- · 2019-06-14 22:55

.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 ( see module class in C#)
If you are used to the VB6 module (method that is accessible wihout an instance), declare the functions as static in c#

查看更多
Animai°情兽
4楼-- · 2019-06-14 22:56

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

A module refers 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.

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.

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.

查看更多
The star\"
5楼-- · 2019-06-14 23:04

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

Visual Basic's Module

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.

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.

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.

查看更多
地球回转人心会变
6楼-- · 2019-06-14 23:10

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.

查看更多
Deceive 欺骗
7楼-- · 2019-06-14 23:14

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

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.

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

查看更多
登录 后发表回答