C#.NET — True or False: A property can be declared inside a class, a struct, or an interface.

Difficulty: Easy

Correct Answer: True

Explanation:

Introduction / Context:This statement checks your knowledge of where properties may be declared in C#. Properties are type members and can appear in several kinds of types.

Given Data / Assumptions:

  • We consider declaration sites for properties in C#.

Concept / Approach:Properties are members of types, not namespaces or methods. C# allows properties in classes, structs, and interfaces. Interfaces declare property signatures (no bodies); classes/structs provide implementations.

Step-by-Step Solution:

Class: property with get/set bodies allowed. Struct: property with get/set bodies allowed (with struct rules). Interface: property signatures allowed (no implementation in classic interfaces).

Verification / Alternative check:Attempting to declare a property in a namespace or inside a method causes a compile-time error.

Why Other Options Are Wrong:They inappropriately restrict where properties can be declared.

Common Pitfalls:Confusing interface property signatures (no bodies) with class/struct implementations.

Final Answer:True

Discussion & Comments

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