Uncategorized

5 Very Useful C# Attributes

Trusted By

Welcome Back! I hope you enjoy the content on this site. If you have not done so already, you may want to subscribe to my RSS feed or become a fan of this blog on Facebook. Thanks for visiting!

DebuggerDisplayAttribute

Namespace: System.Diagnostics

Usage: The DebuggerDisplayAttribute can be a sweet shortcut to avoid expanding the object to get to the value of a given property when debugging. All you have to do is mouse over the object and any property defined in the attribute will show up with it’s value.

image

Sample:

    [DebuggerDisplay("ProductName = {ProductName},ProductSKU= {ProductSKU}")]
    public class Product
   
{
        public string ProductName { get; set; }
        public string ProductSKU { get; set; }
    }


ConditionalAttribute

Namespace: System.Diagnostics

Usage: This is a great attribute to use with code that you only want to run in debug mode. what it does is pretty much is that the body of any method that has the conditional attribute is not compiled when that condition is false.

 

Sample:

        [Conditional("CONDITION1")]
        public static void Method1(int x)
        {
            Console.WriteLine("CONDITION1 is defined");
        }


DebuggerStepThroughAttribute

Namespace: System.Diagnostics

Usage: This attribute is great to skip through methods or properties that only have getters and setters defined.

Sample:

        [DebuggerStepThrough()]
        public virtual int AddressId
        {
            get { return _AddressId;}
           
            set
           
{
               
                _AddressId = value;
               
                OnPropertyChanged("AddressId");
               
            }
           
        }


FlagAttribute

Namespace: System

Usage: Indicates that an enumeration can be treated as a bit field; that is, a set of flags.

Sample:

[Flags] public enum MyColors : short
   
{
        Black = 0,
        Red = 1,
        Green = 2,
        Blue = 4
    }


XmlIgnoreAttribute

Namespace: System.Xml.Serialization

Usage: Tells the the xml serializer to ignore a certain field.

Sample:

public class Group
{
   // The XmlSerializer ignores this field.
  
[XmlIgnore]
   public string Comment;

   // The XmlSerializer serializes this field.
  
public string GroupName;
}

 

Hope this helps.

Haitm

Are we In Control

I talked a while back about Dan Ariely’s book Predictably Irrational: the hidden forces that shape our decisions and today I came upon this talk on TED about some of the example he talks about in his book and some other cognitive illusions we have that are so hard to prove. just watch it it’s a good talk that gives you a different vision on how we really think and make our decisions.

Dan Ariely: Are we in Control

 

Enjoy!