EffectiveCSharp

Card Set Information

Author:
iranye
ID:
157844
Filename:
EffectiveCSharp
Updated:
2015-09-29 12:15:14
Tags:
Sharp Tips Standard Practices
Folders:

Description:
50 ways to improve C# coding
Show Answers:

Home > Flashcards > Print Preview

The flashcards below were created by user iranye on FreezingBlue Flashcards. What would you like to do?


  1. [Item 1]
    Use ________ Instead of Accessible Data Members
    Properties
  2. [Item 1]
    What are some benefits to using Properties?
    • Supports:
    • multi-threading
    • virtual (i.e, all features of a method)
  3. What are Generics?
    Using type parameters (added in 2.0), that let you design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code.
  4. [Item 1]
    Define a generic interface for a name-value pair using Properties.
    • public interface INameValuePair<T>{
    • string Name{get;}
    • T Value{get;set;}
    • }
  5. [Item 1]
    Can Properties be combine with Indexers?
    • Yes.
    • public int this[int index]{
    • get { return theValues[index]; }
    • set { theValues[index] = value; }
    • }
    • // Accessing an indexer:
    • int val = someObject[i];
  6. [Item 2]
    Prefer ________ to const
    readonly
  7. [Item 2]
    const signifies a ___________ constant
    readonly signifies a ____________ constant
    • compile-time
    • runtime
  8. [Item 2]
    Compile-time constants are limited to _____________.
    numbers and strings
  9. [Item 3]
    Prefer the __ or __ Operators to Casts
    is or as
  10. [Item 3]
    What advantage does using the as operator have over casting?
    • As is safer and more efficient at runtime.
    • The benefit applies mostly to user-defined types.
  11. [Item 3]
    user-defined conversions only operate on the __________ type of the object, not the

    ____________ type.
    • compile-time
    • runtime
  12. [Item 4]
    Use ________ Instead of #if
    Conditional Attributes
  13. [Item 4]
    What alternative methodology can be used instead of the #if #endif pragmas?
    • use the Conditional attribute.
    • Use this as shown with a function, which must return void and which ideally isolates
  14. conditional behavior to the function.
    • [Conditional("DEBUG")]
    • private void CheckState()
    • {
    • // Debug statements
    • }
  15. [Item 5]
    Always Provide ________
    ToString()
  16. [Item 5]
    Why should you always define a reasonable override for the ToString() method?
    Without it, it forces users to construct a human-readable representation using Properties
  17. [Item 5]
    Given the following type that implements IFormattable:
    public class Customer : IFormattable
    {
    public string Name {get; set; }
    public string Phone {get; set; }
    }
    Implement the ToString that allows clients to customize like the following:
    customer1.ToString("np", null)
    • string System.IFormattable.ToString(string format, IFormatProvider fp)
    • {
    • switch (format)
    • {
    • case "np":
    • return string.Format("{0, 20}, {1, 15}" Name, Phone);
    • }
    • }
  18. [Item 5]
    In the previous question, what else needs to happen when implementing

    IFormattable.ToString()?
    Must support ToString("G", ...), null, and "" (same as normal ToString())
  19. [Item 6]
    Understand the ________ Among the Many Different Concepts of Equality
    Relationships
  20. [Item 6]
    What are four ways to define Equality Relationship.
    • public static bool ReferenceEquals(object left, object right);
    • public static bool Equals(object left, object right);
    • public virtual bool Equals(object right);
    • public static bool operator ==(MyClass left, MyClass right);
  21. [Item 7]
    Understand the Pitfalls of ________
    GetHashCode()
  22. [Item 8]
    Prefer Query Syntax to ________
    Loops
  23. [Item 9]
    Avoid ________ Operators in your APIs. What is one reason to do so?
    • Conversion
    • Could end up having to force users to perform casts.
  24. [Item 10]
    Use Optional Parameters to Minimize ________
    Method Overloads
  25. [Item 11]
    Understand the Attraction of ________ and explain.
    • Small Functions
    • When the .Net runtime invokes the JIT compiler to translate the IL generated by the C# compiler into machine code, the CLR invokes the JITer on a function-by-function basis. Functions that don't get called, don't get JITed. Therefore, writing more small functions instead of fewer big functions reduces the amount of code that has to be JITed at runtime.
  26. [Item 12]
    Prefer ________ to Assignment Statements. In what 3 cases should you not do this?
    • Member Initializers
    • I.e., initialze member variables when you declare them instead of in the Constructor.
    • 1. When initializing the object to 0 or null
    • 2. When creating multiple initializations for the same object
    • 3. When you need to facilitate exception handling
  27. [Item 13]
    Use Proper Initialization for ________ Class Members (e.g., Singleton)
    Static
  28. [Item 14] TODO
    Minimize Duplicate ________ Logic. What is the order of operations for constructing the

    first instance of a type?
    • Initialization
    • 1. Static variable storage is set to 0
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
  29. [Item 15]
    Utilize using and try/finally for ________
    Resource Cleanup
  30. True or False? It is the responsibility of the system to Dispose of Types that use

    unmanaged system resources to.
    False. It is the responsibility of the code that uses the type to Dispose.
  31. [Item 16]
    Avoid Creating ________ Objects
    Unnecessary
  32. [Item 17]
    Implement the Standard Dispose ________
    Pattern
  33. [Item 18] TODO
    Distinguish between ________ Types and ________ Types
    • Value and Reference
    • Value types store values and Reference types define behavior
  34. [Item 19]
    Ensure that ________ is a Valid State for a Value Type.
    0
  35. [Item 19]
    Never create an ________ that doesn't include 0 as a valid choice
    enum
  36. [Item ]
    ________

What would you like to do?

Home > Flashcards > Print Preview