开发者

What are classes and modules for in C#

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.


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.


(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


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.


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.


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.


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.


.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#

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜