What is the "module" keyword in C# .NET?
Asked Answered
S

1

7

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
{
}
Stockade answered 29/4, 2020 at 18:19 Comment(1)
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://mcmap.net/q/218559/-what-is-a-module-in-net; learn.microsoft.com/en-us/visualstudio/test/…Soothsay
R
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.

Recurve answered 29/4, 2020 at 18:31 Comment(2)
Thank you, This is what I was looking for :)Stockade
Specifically, the module keyword when used this way applies an attribute to what the .NET Runtime calls a "module". In the vast, vast, vast majority of cases, each assembly has only one module, so most developers don't even know about modules.Triangulation

© 2022 - 2024 — McMap. All rights reserved.