C#.NET — Pick the correct syntax to apply an attribute to a class (example: Serializable).

Difficulty: Easy

Correct Answer: [ Serializable() ] class sample { /* ... */ }

Explanation:

Introduction / Context:Attributes in C# use square-bracket syntax and are placed immediately before the declaration they annotate. This question checks recognition of the precise syntax.

Given Data / Assumptions:

  • Attribute being applied: Serializable.
  • Target: a class declaration.

Concept / Approach:The correct form is: [Serializable] or [Serializable()] placed before the class declaration. Parentheses are optional if there are no arguments.

Step-by-Step Solution:

Reject angle-bracket forms (not C# attribute syntax).Reject parenthesized-only forms; attributes require square brackets.Accept square-bracket form: [ Serializable() ] class sample { ... }

Verification / Alternative check:Compile a minimal class annotated with [Serializable] and confirm no errors; other forms will cause syntax errors.

Why Other Options Are Wrong:Angle brackets and bare parentheses are not valid C# attribute delimiters; misspellings like “Serializablef)” are invalid.

Common Pitfalls:Confusing XML-like angle-brackets with C# attribute brackets; forgetting that parentheses are optional when there are no parameters.

Final Answer:[ Serializable() ] class sample { /* ... */ }

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion