C#.NET — Is it allowed to declare an empty struct (no fields or members)?
C# Programming
Structures
Difficulty: Easy
Choose an option
-
ATrue
-
BFalse
-
C—
-
D—
-
E—
Answer
Correct Answer: True
Explanation
Introduction / Context:An “empty struct” is a value type declared without fields or members. This is occasionally used for tagging or interop scenarios.
Given Data / Assumptions:
- We are using standard C# language rules.
- The struct has no fields, properties, or methods.
Concept / Approach:C# allows declaring an empty struct, for example struct Marker { }. The runtime assigns it a nonzero size for technical reasons (implementation-dependent), but the declaration itself is valid.
Step-by-Step Solution:
Declare struct S { } → legal syntax.Use S as a type parameter constraint or as a marker value type if needed.Verification / Alternative check:Compile a project with an empty struct; it compiles without errors.
Why Other Options Are Wrong:
- False: Suggests the language forbids empty structs, which is not the case.
Common Pitfalls:Expecting empty structs to have zero size in memory; they do not, due to alignment and CLI rules.
Final Answer:True