6

I am learning C# and came across the keyword module. I would like to know what this module keyword in C# is and how it is useful. For example, consider the below code:

[module: Test]
public class TestAttribute : Attribute
{
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
gnagesh
  • 83
  • 6
  • It looks like you are viewing a unit test in C# and [module: Test] seems to state that the TestAttribute class is part of the Test module. I'm not a .NET developer, but from what I have read, a module seems to be a logical collection of code within an Assembly. The syntax may be part of the testing framework. Suggested reference links: https://stackoverflow.com/questions/645728/what-is-a-module-in-net; https://learn.microsoft.com/en-us/visualstudio/test/unit-test-basics?view=vs-2019 – cadebe May 22 '20 at 13:36

1 Answers1

6

In your example module is a way to specify the attribute usage, like this:

[module: CLSCompliant(true)]
int Method1() { return 0; }

It is also called attribute target:

The target of an attribute is the entity which the attribute applies to. For example, an attribute may apply to a class, a particular method, or an entire assembly. By default, an attribute applies to the element that follows it.

For the full list of C# attribute parameters check the official documentation.

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
  • 1
    Thank you, This is what I was looking for :) – gnagesh Apr 29 '20 at 18:37
  • 2
    Specifically, the `module` keyword when used this way applies an attribute to what the .NET Runtime calls a ["module"](https://stackoverflow.com/questions/645728/what-is-a-module-in-net). In the vast, vast, vast majority of cases, each assembly has only one module, so most developers don't even know about modules. – Joe Sewell Apr 29 '20 at 18:46