2,000 Things You Should Know About C#

Everything a C# Developer Needs to Know, in Bite-Sized Chunks

  • Home
  • About
  • Index
Posts Comments
  • Basics
  • Classes
  • Data Types
  • Methods
  • Exceptions
  • Visual Studio
  • Strings
  • Miscellaneous
  • Operators
  • Statements

#469 – Attaching an Attribute to a Type Member

December 6, 2011 Leave a comment

An attribute describes metadata that should be associated with a particular type member.

Below is an example of attaching the Obsolete attribute to the Cow.FeedCowInBarn method.  A developer can use this attribute to mark a method as being obsolete or deprecated, indicating that it should no longer be called.

You attach an attribute to a type member by using a square bracket followed by the name of the attribute and then any arguments that the attribute takes.  The Obsolete attribute takes a single argument that will be displayed at compile time when code tries to call the associated method.

        [Obsolete("Feeding cows indoors is no longer recommended.  Instead, call the LetGrazeOutside() method.")]
        public void FeedCowInBarn()
        {
            Console.WriteLine("Cow eats slop in dim confines of barn");
        }

Intellisense now tells us that the method is deprecated.

We also see the specified message at compile-time.

Advertisement

Filed under Basics Tagged with Attributes, Basics, C#, Metadata, Obsolete, ObsoleteAttribute

Sean Sexton

Recent Posts

  • #1,219 – C# 6.0 – Filtering Exceptions
  • #1,218 – C# 6.0 – Using Expression-Bodied Property Getters
  • #1,217 – C# 6.0 – Using Expression-Bodied Methods
  • #1,216 – C# 6.0 – Initializing Read-Only Auto-Properties from Constructors
  • #1,215 – C# 6.0 – New Syntax for Dictionary Initializers

Blogroll

  • 2,000 Things You Should Know About WPF
  • Britannica Geek
  • Sean on Twitter
  • Sean's Stuff

Calendar

December 2011
S M T W T F S
 123
45678910
11121314151617
18192021222324
25262728293031
« Nov   Jan »

Top Posts

  • Index
  • #443 - An Interface Cannot Contain Fields
  • #349 - The Difference Between Virtual and Non-Virtual Methods
  • #782 - You Can Create an Instance of a struct Without the new Keyword
  • #276 - Passing a struct by Reference
  • #2 - The Smallest Possible C# Program
  • #982 - An Enum Type Can Store a Maximum of 32 Flags
  • #815 - Named vs. Positional Arguments
  • #831 - Implementing a Copy Constructor in a Derived Class
  • #257 - Private Static Fields

Tags

#define #if 6.0 Accessibility Anonymous Types Arguments Array Arrays Basics Boxing C# catch Class Class Diagram Classes Collections Constants Constructor Constructors Conversions Covariance Data Types Deep Copy Delegate Delegates Enum Enumeration Equals Events Exceptions Fields Finalizer Floating Point foreach Garbage Collection Generic Generics Heap IEnumerable Inheritance Intellisense Interface Interfaces Lambda Expression Main Memory Methods Miscellaneous Namespaces new null Nullable Object Object Initializer Operators Optional Parameters override Parameters Patterns Polymorphism Private Properties Public Reference Types Statements Static Strings struct Throw try Unicode Value Types Variables virtual Visual Studio

Blog Stats

  • 2,831,955 hits

Blog at WordPress.com.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Follow Following
    • 2,000 Things You Should Know About C#
    • Join 367 other followers
    • Already have a WordPress.com account? Log in now.
    • 2,000 Things You Should Know About C#
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...