C# - Attributes Questions and Answers
C# Attributes programming questions and answers help developers understand metadata and reflection in .NET applications. Attributes provide declarative information about program elements, influencing how the runtime behaves. These concepts are often asked in interviews by companies like HCL, Infosys, and TCS to assess deep understanding of advanced C# features. Practicing programming interview questions with answers on attributes helps candidates learn how to create custom attributes, retrieve metadata using reflection, and apply predefined ones like [Obsolete] or [Serializable]. Strengthening this topic ensures you can confidently tackle conceptual and coding questions in software development interviews.
Focus on attributes and modifiers in C programming language. Also try C functions and subroutines and C datatypes
C# - Attributes
21. Which attribute indicates that a class should not be inherited?
- NonInheritable
- Final
- Explanation: sealed prevents a class from being inherited, though it is not an attribute.
- Obsolete
22. Which attribute marks a program element as outdated?
- Deprecated
- Old
- Explanation: Obsolete warns that a member should no longer be used.
- Legacy
23. What happens when an Obsolete attribute is marked as error = true?
- Warning is ignored
- Program fails at runtime
- Explanation: Setting error = true causes a compilation error when used.
- Attribute is skipped
24. Which attribute hides a public member from IntelliSense?
- Hidden
- Explanation: EditorBrowsable controls visibility in IntelliSense.
- NonBrowsable
- Ignore
25. Which attribute specifies a method that should run before a test?
- PreTest
- TestInit
- Explanation: TestInitialize is used in unit testing frameworks.
- Setup
26. Which reflection class is commonly used to read attributes at runtime?
- AttributeReader
- Explanation: The Type class is used with reflection to retrieve attributes.
- AssemblyInfo
- MetaData
27. Which method retrieves attributes applied to a program element?
- GetAttribute()
- ReadAttributes()
- Explanation: GetCustomAttributes() is used to obtain attribute instances.
- FetchAttributes()
28. Which attribute indicates a method does not return a value?
- Void
- NoReturn
- Explanation: DoesNotReturn informs the compiler and analyzers.
- Returnless
29. Which attribute is applied at the assembly level?
- Explanation: AssemblyTitle provides metadata about the assembly.
- AssemblyUsage
- AssemblyInfo
- AssemblyScope
30. Which attribute specifies the version of an assembly?
- AssemblyBuild
- AssemblyInfo
- Explanation: AssemblyVersion defines the assembly version.
- AssemblyLevel