Effective c# part1

Uploaded on

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

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 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 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. 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. 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. 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. 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. 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