SlideShare une entreprise Scribd logo
1  sur  10
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

Contenu connexe

Tendances

New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 
TypeScript Presentation
TypeScript PresentationTypeScript Presentation
TypeScript Presentation
Patrick John Pacaña
 
JavaScript: Patterns, Part 2
JavaScript: Patterns, Part  2JavaScript: Patterns, Part  2
JavaScript: Patterns, Part 2
Chris Farrell
 

Tendances (20)

C++ to java
C++ to javaC++ to java
C++ to java
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 
Why Static Type Checking is Better
Why Static Type Checking is BetterWhy Static Type Checking is Better
Why Static Type Checking is Better
 
FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)
 
TypeScript Presentation
TypeScript PresentationTypeScript Presentation
TypeScript Presentation
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Start with swift
Start with swiftStart with swift
Start with swift
 
Introduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviIntroduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston Levi
 
New c sharp4_features_part_v
New c sharp4_features_part_vNew c sharp4_features_part_v
New c sharp4_features_part_v
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
 
TypeScript intro
TypeScript introTypeScript intro
TypeScript intro
 
TypeScript Best Practices
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practices
 
Doppl development iteration #7
Doppl development   iteration #7Doppl development   iteration #7
Doppl development iteration #7
 
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
 
TypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersTypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack Developers
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
 
Functional Groovy - Confess
Functional Groovy - ConfessFunctional Groovy - Confess
Functional Groovy - Confess
 
Typescript Fundamentals
Typescript FundamentalsTypescript Fundamentals
Typescript Fundamentals
 
JavaScript: Patterns, Part 2
JavaScript: Patterns, Part  2JavaScript: Patterns, Part  2
JavaScript: Patterns, Part 2
 

Similaire à Effective c# part1

Android Open source coading guidel ine
Android Open source coading guidel ineAndroid Open source coading guidel ine
Android Open source coading guidel ine
Pragati Singh
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for java
maheshm1206
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
Connex
 

Similaire à Effective c# part1 (20)

c# at f#
c# at f#c# at f#
c# at f#
 
C# AND F#
C# AND F#C# AND F#
C# AND F#
 
C++ Training
C++ TrainingC++ Training
C++ Training
 
Perfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standardsPerfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standards
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
C sharp
C sharpC sharp
C sharp
 
C#ppt
C#pptC#ppt
C#ppt
 
Android Open source coading guidel ine
Android Open source coading guidel ineAndroid Open source coading guidel ine
Android Open source coading guidel ine
 
Android coding standard
Android coding standard Android coding standard
Android coding standard
 
Module 2: C# 3.0 Language Enhancements (Slides)
Module 2: C# 3.0 Language Enhancements (Slides)Module 2: C# 3.0 Language Enhancements (Slides)
Module 2: C# 3.0 Language Enhancements (Slides)
 
Language tour of dart
Language tour of dartLanguage tour of dart
Language tour of dart
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)
 
Designing Better API
Designing Better APIDesigning Better API
Designing Better API
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for java
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
 
CORBA IDL
CORBA IDLCORBA IDL
CORBA IDL
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
 
Dry-wit Overview
Dry-wit OverviewDry-wit Overview
Dry-wit Overview
 
C Course Material0209
C Course Material0209C Course Material0209
C Course Material0209
 

Dernier

Dernier (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

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