tags:

views:

458

answers:

6

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.

+5  A: 

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.

Nick Craver
+3  A: 

(May be 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, its 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

Vivek Bernard
There are modules :) `typeof(string).Module` for example...you can see how they're arranged via reflection: http://msdn.microsoft.com/en-us/library/system.reflection.module.aspx Maybe you mean there's no VB 6 equivalent?
Nick Craver
@Nick thats exactly what i meant, seeing his question I thought he was trying to find an equivalent of VB.net's "Module", which is very different from System.Reflection.Module ...Doh ten reps less for nothing... :)
Vivek Bernard
Not sure who down-voted you, but it's -2 rep not -10 :) Maybe the author will clarify what they meant since what one assumes "module" must mean is severely influenced by their background. +1 from me, your corrected answer is perfectly valid.
Nick Craver
@Nick thanks, you're kind :)...can i buy you a beer
Vivek Bernard
You answered my question. Thanks Vivek. I was trying to find the C# equivalent. Thanks
Arlen Beiler
@Arlen Beiler, Glad to know that :)
Vivek Bernard
+2  A: 

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.

Aaronaught
+1  A: 
Kb
This is the second most helpful question. Thanks
Arlen Beiler
+1  A: 

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.

John Saunders
Just to point that VB.NET also has Modules as the VB6 modules.
Romias
@Romias: I know. I was talking about C#.
John Saunders
+8  A: 

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.

JaredPar