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
31. Which attribute allows conditional compilation?
- Explanation: Conditional includes method calls based on compilation symbols.
- IfDefined
- DebugOnly
- CompileCheck
32. Which attribute is used to define serialization behavior?
- Explanation: Serializable marks a class as eligible for serialization.
- Data
- Persist
- Store
33. Which attribute prevents a class from being serialized?
- Explanation: NonSerialized prevents a field from being serialized.
- NoSerialize
- IgnoreData
- SkipSerialize
34. Which attribute specifies a default value for a property?
- Default
- Value
- Explanation: DefaultValue provides design-time default values.
- InitValue
35. Which attribute is used to specify code analysis suppression?
- Suppress
- IgnoreWarning
- Explanation: SuppressMessage disables specific code analysis warnings.
- CodeIgnore
36. Which attribute helps control COM visibility?
- Explanation: ComVisible specifies whether a type is visible to COM.
- Interop
- ComAccess
- ExposeCom
37. Which attribute specifies thread behavior of a method?
- ThreadSafe
- Explanation: STAThread specifies single-threaded apartment model.
- MultiThread
- ThreadMode
38. When are attributes evaluated in C#?
- Only at compile time
- Only at runtime
- Explanation: Attributes are compiled into metadata and accessed at runtime using reflection.
- Only during debugging