Effective c# part1
Upcoming SlideShare
Loading in...5

Effective c# part1



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

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



Total Views
Views on SlideShare
Embed Views



18 Embeds 308

http://yuriyseniuk.blogspot.com 263
https://yuriyseniuk.blogspot.com 6
http://yuriyseniuk.blogspot.com.au 6
http://yuriyseniuk.blogspot.co.uk 5
http://yuriyseniuk.blogspot.ru 4
http://yuriyseniuk.blogspot.de 4
http://yuriyseniuk.blogspot.cz 3
http://yuriyseniuk.blogspot.fr 3
http://yuriyseniuk.blogspot.ca 3
http://yuriyseniuk.blogspot.in 2
http://yuriyseniuk.blogspot.co.il 2
http://yuriyseniuk.blogspot.it 1
http://yuriyseniuk.blogspot.com.br 1
http://yuriyseniuk.blogspot.com.es 1
http://yuriyseniuk.blogspot.gr 1
http://translate.googleusercontent.com 1
http://yuriyseniuk.blogspot.com.tr 1
http://yuriyseniuk.blogspot.se 1



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Effective c# part1 Effective c# part1 Presentation Transcript

  • 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