Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are classes and modules for in C#

Tags:

c#

module

class

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.

like image 445
Arlen Beiler Avatar asked Jan 30 '10 16:01

Arlen Beiler


3 Answers

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.

like image 147
JaredPar Avatar answered Oct 16 '22 17:10

JaredPar


(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

like image 38
Vivek Bernard Avatar answered Oct 16 '22 17:10

Vivek Bernard


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.

like image 8
Nick Craver Avatar answered Oct 16 '22 18:10

Nick Craver