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

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
ASP.NET Best Practices Dawit Wubshet Park University.
Exception Handling Topics We will discuss the following main topics: – Handling Exceptions – Throwing Exceptions – More about Input/Output Streams.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 10 Classes Continued
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Object Oriented Programming
Chapter 12: Exception Handling
Understanding Events and Exceptions Lesson 3. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understand events and event handling Understand.
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
File I/O Applied Component-Based Software Engineering File I/O CSE 668 / ECE 668 Prof. Roger Crawfis.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Object Oriented Programming Elhanan Borenstein Lecture #4.
Initialization & Cleanup Guaranteed initialization with the constructor The name of the constructor is the same as the name of the class.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
ISBN Object-Oriented Programming Chapter Chapter
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
Swing Threading Nested Classes COMP 401 Fall 2014 Lecture 23 11/25/2014.
Effective C# 50 Specific Ways to Improve Your C# Item 12 ~ 13 Neo2012/07/25 1.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Object Oriented Programming in Java Habib Rostami Lecture 10.
C# Exceptions 1 CNS 3260 C#.NET Software Development.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Exceptions in the Java programming language J. W. Rider.
The Object-Oriented Thought Process Chapter 03
Jim Fawcett CSE681 – SW Modeling & Analysis Fall 2014
Object Oriented Programming using Java - Class Instance Variables
Functional Programming with Java
Packages and Interfaces
Finalization 17: Finalization
Which best describes the relationship between classes and objects?
Quiz Points 1 Rules Raise your hand if you know the question
Review of Previous Lesson
Jim Fawcett CSE681 – SW Modeling & Analysis Fall 2018
SPL – PS3 C++ Classes.
Presentation transcript:

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

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

UTILIZE CONSTRUCTOR CHAINING Item 14:

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!

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

Way 1: One constructor to call another constructor

Way 2: Calling a function for constructor initialization

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

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

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?

The order of operations for constructing the first instance of a type 1. Static variable storage is set to Static variable storage is set to 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 Instance variable storage is set to 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.

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

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

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

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

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

Example (3/3)

The using statement generates a TRy/finally block

using statement needs IDisposable interface (1/2)

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

Every using statement creates a new nested TRy / finally block

Improvement

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

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().

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