EFFECTIVE C# PART1

               yuriyseniuk.blogspot.com   Yuriy Seniuk
Part1. Agenda
   Item 1: Use Properties Instead of
    Accessible Data Members
   Item 2: Prefer readonly to const
   Item 3: Prefer the is or as Operators to
    Casts
   Item 4: Use Conditional Attributes Instead
    of #if
   Item 5: Always Provide ToString()
1. Properties
   Properties have the same performance as
    fields;
   Properties can be bound, Fields – not;
   Properties are useful during thread
    synchronization;
   Properties can be virtual;
   Properties can be used in interfaces;
   Properties can have different access levels;
   Property syntax used to create indexers;
   Indexers can be multidimensional;
1. Properties
   Indexers can use non integer parameters;
   Class can contains several indexers;
   Fields can not be substituted by properties due
    it’s binary incompatibility.
2. Prefer readonly to const
   Readonly – runtime constants;
   Const – compile time constants;
   --------------------------------------
   Compile time constants is slightly faster;
   Const can be used only with value types;
   Const must be used when in future releases it
    never be changed.
3. Prefer is/as operator to Cast
   is/as does not perform any user conversions;
   as can be applied only to reference types;
   is can be used for verification of types
    capabilities;
   foreach uses non generic IEnumerable
    interface and cast;
   .Cast<T>();
   .OfType<T>();
   Microsoft asks to avoid user defined casts;
4. Use Conditional Attr insted of
#if
   #if/#endif used to produce different builds;
   Using Conditional attr you fully isolates your code;
    Log() --- this method creates empty code invocation.
    {
        #if DEBUG
        //some code
        #endif
    }

    [Conditional(“DEBUG”)]
    Log()
    {
         //some code
    }
4. Use Conditional Attr insted of
#if
   Conditional attributes can be combined:
    [Conditional(“DEBUG”), Conditional(“TRACE”)]

   It is possible to combine attributes on old way:
    #if(DEBUG && TRACE)
    #define BOTH
    #endif

   Conditional attributes can be used in methods
    that returns void;
4. Use Conditional Attr insted of
#if
   Do not use parameters in conditional methods
    [Conditional(“DEBUG”)]
    log(string mess)
    {
    }


    to avoid this one
    string mess = string.Empty;
    log(mess = GetMessage()); //method will never be executed in DEBUG build.
    Console.WriteLine(mess);
5. Always provide ToString()
   Override ToString() to create a reasonable
    human-readable class representation;
   In anonymous types ToString() displays the
    value of each property;
   Use IFormatable for complicated types;
   Support “G”, String.Empty and null in
    IFormatable;
   Use IFormatProvider to enhance closed
    libraries.
   IFormatProvider can be used with types that is
    not inherited from IFormatable

Effective c# part1

  • 1.
    EFFECTIVE C# PART1 yuriyseniuk.blogspot.com Yuriy Seniuk
  • 2.
    Part1. Agenda  Item 1: Use Properties Instead of Accessible Data Members  Item 2: Prefer readonly to const  Item 3: Prefer the is or as Operators to Casts  Item 4: Use Conditional Attributes Instead of #if  Item 5: Always Provide ToString()
  • 3.
    1. Properties  Properties have the same performance as fields;  Properties can be bound, Fields – not;  Properties are useful during thread synchronization;  Properties can be virtual;  Properties can be used in interfaces;  Properties can have different access levels;  Property syntax used to create indexers;  Indexers can be multidimensional;
  • 4.
    1. Properties  Indexers can use non integer parameters;  Class can contains several indexers;  Fields can not be substituted by properties due it’s binary incompatibility.
  • 5.
    2. Prefer readonlyto const  Readonly – runtime constants;  Const – compile time constants;  --------------------------------------  Compile time constants is slightly faster;  Const can be used only with value types;  Const must be used when in future releases it never be changed.
  • 6.
    3. Prefer is/asoperator to Cast  is/as does not perform any user conversions;  as can be applied only to reference types;  is can be used for verification of types capabilities;  foreach uses non generic IEnumerable interface and cast;  .Cast<T>();  .OfType<T>();  Microsoft asks to avoid user defined casts;
  • 7.
    4. Use ConditionalAttr insted of #if  #if/#endif used to produce different builds;  Using Conditional attr you fully isolates your code; Log() --- this method creates empty code invocation. { #if DEBUG //some code #endif } [Conditional(“DEBUG”)] Log() { //some code }
  • 8.
    4. Use ConditionalAttr insted of #if  Conditional attributes can be combined: [Conditional(“DEBUG”), Conditional(“TRACE”)]  It is possible to combine attributes on old way: #if(DEBUG && TRACE) #define BOTH #endif  Conditional attributes can be used in methods that returns void;
  • 9.
    4. Use ConditionalAttr insted of #if  Do not use parameters in conditional methods [Conditional(“DEBUG”)] log(string mess) { } to avoid this one string mess = string.Empty; log(mess = GetMessage()); //method will never be executed in DEBUG build. Console.WriteLine(mess);
  • 10.
    5. Always provideToString()  Override ToString() to create a reasonable human-readable class representation;  In anonymous types ToString() displays the value of each property;  Use IFormatable for complicated types;  Support “G”, String.Empty and null in IFormatable;  Use IFormatProvider to enhance closed libraries.  IFormatProvider can be used with types that is not inherited from IFormatable