Effective c# part1

  • 1,630 views
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

Views

Total Views
1,630
On Slideshare
0
From Embeds
0
Number of Embeds
13

Actions

Shares
Downloads
21
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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