Presentation is loading. Please wait.

Presentation is loading. Please wait.

Effective C# 50 Specific Ways to Improve Your C# Item 14~15 2012/08/14 1.

Similar presentations


Presentation on theme: "Effective C# 50 Specific Ways to Improve Your C# Item 14~15 2012/08/14 1."— Presentation transcript:

1 Effective C# 50 Specific Ways to Improve Your C# Item 14~15 2012/08/14 1

2 Agenda Item 14: Utilize Constructor Chaining Item 14: Utilize Constructor Chaining Item 15: Utilize using and TRy/finally for Resource Cleanup Item 15: Utilize using and TRy/finally for Resource Cleanup

3 UTILIZE CONSTRUCTOR CHAINING Item 14:

4 Stop to copy paste for initialize class How to satisfy the multiple overrides defined in the class interface? Common answer: Create first constructor and then copy and paste the code into other constructors STOP IT! How to satisfy the multiple overrides defined in the class interface? Common answer: Create first constructor and then copy and paste the code into other constructors STOP IT!

5 C# does not support default parameters C++ public MyClass(int c= 1) : initilaCount (c) {}//------------------------------------------------ //myClass.initilaCount = 1 MyClass *pmyClass = new MyClass(); //-- or – //myClass.initilaCount =2 MyClass *pmyClass = new MyClass(2);

6 Way 1: One constructor to call another constructor

7 Way 2: Calling a function for constructor initialization

8 In IL way to view (1/2) IL Source code

9 In IL way to view (2/2) The compiler does not generate multiple calls to the base class constructor

10 Another issue while you are using a function to constructor initialization Even you don’t care previous issue (multi initialization generated) How about this? Even you don’t care previous issue (multi initialization generated) How about this?

11 The order of operations for constructing the first instance of a type 1. Static variable storage is set to 0. 1. Static variable storage is set to 0. 2. Static variable initializers execute. 2. Static variable initializers execute. 3. Static constructors for the base class execute. 3. Static constructors for the base class execute. 4. The static constructor executes. 4. The static constructor executes. 5. Instance variable storage is set to 0. 5. Instance variable storage is set to 0. 6. Instance variable initializers execute. 6. Instance variable initializers execute. 7. The appropriate base class instance constructor executes. 7. The appropriate base class instance constructor executes. 8. The instance constructor executes. 8. The instance constructor executes.

12 Summary Don’t use a function to initialize the constructor Don’t use a function to initialize the constructor

13 UTILIZE USING AND TRY/FINALLY FOR RESOURCE CLEANUP Item 15:

14 Unmanaged system resources Should be explicitly released using the Dispose() method of the IDisposable interface Should be explicitly released using the Dispose() method of the IDisposable interface The best way to ensure that Dispose() always gets called The best way to ensure that Dispose() always gets called – using statement – try/finally block If you forget to dispose of those items, those nonmemory resources are freed later If you forget to dispose of those items, those nonmemory resources are freed later

15 Example (1/3) Two disposable objects are not properly cleaned up in this example: SqlConnection and SqlCommand.

16 Example (2/3) If exception occurs,..

17 Example (3/3)

18 The using statement generates a TRy/finally block

19 using statement needs IDisposable interface (1/2)

20 using statement needs IDisposable interface (2/2) The using statement works only if the compile-time type supports the IDisposable interface The using statement works only if the compile-time type supports the IDisposable interface

21 Every using statement creates a new nested TRy / finally block

22 Improvement

23 It looks cleaner, but it has a subtle bug If exception occurs,..

24 Close method to free resources Some types support both a Dispose method and a Close method to free resources Some types support both a Dispose method and a Close method to free resources The Dispose method does more than free resources The Dispose method does more than free resources – Dispose calls GC.SuppressFinalize(). – Close typically does not call GC.SuppressFinalize().

25 Summary Less than 100 classes in the.NET Framework implement Idisposable that's out of more than 1,500 types Less than 100 classes in the.NET Framework implement Idisposable that's out of more than 1,500 types Wrap objects in using clauses or TRy/finally blocks Wrap objects in using clauses or TRy/finally blocks


Download ppt "Effective C# 50 Specific Ways to Improve Your C# Item 14~15 2012/08/14 1."

Similar presentations


Ads by Google