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 ...
1. Properties   Properties have the same performance as    fields;   Properties can be bound, Fields – not;   Propertie...
1. Properties   Indexers can use non integer parameters;   Class can contains several indexers;   Fields can not be sub...
2. Prefer readonly to const   Readonly – runtime constants;   Const – compile time constants;   -----------------------...
3. Prefer is/as operator to Cast   is/as does not perform any user conversions;   as can be applied only to reference ty...
4. Use Conditional Attr insted of#if   #if/#endif used to produce different builds;   Using Conditional attr you fully i...
4. Use Conditional Attr insted of#if   Conditional attributes can be combined:    [Conditional(“DEBUG”), Conditional(“TRA...
4. Use Conditional Attr insted of#if   Do not use parameters in conditional methods    [Conditional(“DEBUG”)]    log(stri...
5. Always provide ToString()   Override ToString() to create a reasonable    human-readable class representation;   In a...
Upcoming SlideShare
Loading in …5
×

Effective c# part1

2,039 views

Published on

Slides are based on "Effective C#. 2nd edition. Bill Wagner" book.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Effective c# part1

  1. 1. EFFECTIVE C# PART1 yuriyseniuk.blogspot.com Yuriy Seniuk
  2. 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. 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. 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. 5. 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.
  6. 6. 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;
  7. 7. 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 }
  8. 8. 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;
  9. 9. 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);
  10. 10. 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

×