Effective C# 50 Specific Ways to Improve Your C# Item 12 ~ 13 Neo2012/07/25 1.

Slides:



Advertisements
Similar presentations
Lilian Blot VARIABLE SCOPE EXCEPTIONS FINAL WORD Final Lecture Spring 2014 TPOP 1.
Advertisements

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
CSE 332: C++ copy control I Copy Control (Part I) Copy control consists of 5 distinct operations –A copy constructor initializes an object by duplicating.
Rounding Out Classes The objectives of this chapter are: To discuss issues surrounding passing parameters to methods What is "this"? To introduce class.
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
資料結構實習-一 參數傳遞.
OBJECTS AND CLASSES Outline Reference Variables and Reference Types Primitive Types and Reference Types Static Variable, Constants,
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
Reference Types. 2 Objectives Introduce reference types –class –array Discuss details of use –declaration –allocation –assignment –null –parameter –aggregation.
中序轉後序 藉由由左向右掃瞄中序運算式產生後序運算式,遇到 運算元就直接輸出,遇到運算符號則先存入堆疊,將 優先權較高者輸出。 範例: a + b * c TokenStack [0] [1] [2] topoutput aa ++0a b+0ab *+ *1ab c+ *1abc eosabc*+
What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;
幼兒行為觀察與記錄 第八章 事件取樣法.
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.
Ch 4. Memory Management Timothy Budd Oregon State University.
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.
(…A FEW OF THEM) C++ DESIGN PATTERNS. WHAT ARE THEY? Commonly occurring constructs Could be part of good software engineering Not universally agreed Good.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
CSC Programming I Lecture 8 September 9, 2002.
Effective Java: Creating and Destroying Objects Last Updated: Fall 2012.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Data Structures (1st Exam). 1.[5] Suppose there are only two constructors for a class, one that passes in a single integer parameter called amount, for.
Effective C# 50 Specific Ways to Improve Your C# Item 14~ /08/14 1.
Internet Software Development Controlling Threads Paul J Krause.
Object-Oriented Programming in C++
Programming in Java CSCI-2220 Object Oriented Programming.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Effective C# 50 Specific Ways to Improve Your C# Item 18~ /08/15 1.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Singleton Duchenchuk Volodymyr Oksana Protsyk. 2 /48.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
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.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CIS162AD Constructors Part 2 08_constructors.ppt.
1 Introduction to Object Oriented Programming Chapter 10.
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.
Constructors and Destructors
Learning Objectives Pointers as dada members
Overview 4 major memory segments Key differences from Java stack
Jim Fawcett CSE681 – SW Modeling & Analysis Fall 2014
Static data members Constructors and Destructors
Programming with ANSI C ++
Dynamically Allocated Memory
Overview 4 major memory segments Key differences from Java stack
Basic C++ What’s a declaration? What’s a definition?
understanding memory usage by a c++ program
Programming Design Patterns
Finalization 17: Finalization
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Constructors and Destructors
The University of Texas – Pan American
Jim Fawcett CSE681 – SW Modeling & Analysis Fall 2018
Presentation transcript:

Effective C# 50 Specific Ways to Improve Your C# Item 12 ~ 13 Neo2012/07/25 1

Agenda.NET Resource Management.NET Resource Management Item 12: Prefer Variable Initializers to Assignment Statements Item 12: Prefer Variable Initializers to Assignment Statements Item 13: Initialize Static Class Members with Static Constructors Item 13: Initialize Static Class Members with Static Constructors

.NET Resource Management The Garbage Collector (GC) controls managed memory for you. The Garbage Collector (GC) controls managed memory for you. – Runs in its own thread to remove unused memory from your program. – Make contiguous block of memory.

.NET Resource Management Don’t use Destructor like the C++ Don’t use Destructor like the C++ – In the previous example, the code eventually exits the critical section, but, in C#, it doesn't exit the critical section when the function exits. – Relying on finalizers also introduces performance penalties. // Good C++, bad C#: class CriticalSection { public: // Constructor acquires the system resource. CriticalSection( ) { EnterCriticalSection( ); } // Destructor releases system resource. ~CriticalSection( ) { ExitCriticalSection( ); } }; // usage: void Func( ) { // The lifetime of s controls access to // the system resource. CriticalSection s; // Do work. //... // compiler generates call to destructor. // code exits critical section. }

.NET Resource Management Memory leaks and a host of other pointer-related problems are no longer your problem Memory leaks and a host of other pointer-related problems are no longer your problem – Use finalizers to ensure proper cleanup of those non- memory resources.

Prefer Variable Initializers to Assignment Statements Item 12:

What is the different between them? Initializers Initializers – Assign value at definition time – Avoid mistakes Assignment Statements Assignment Statements – Assign(Decide) value in constructor – Facilitate exception handling

Why we use Initializers? Initializers can help to avoid human error Initializers can help to avoid human error – Case: Multiple constructors

Exceptional Cases Case 1: Don’t use Initializers on value type Case 1: Don’t use Initializers on value type – Initializers set memory to 0 – Initobj also set memory to 0 – It’s like do nothing….class public auto ansi beforefieldinit MyClass extends [mscorlib]System.Object {.method public hidebysig specialname rtspecialname instance void.ctor() cil managed {.maxstack 8 L_0000: ldarg.0 L_0001: ldflda valuetype AssemblyTest.MyValType AssemblyTest.MyClass::MyVal2 L_0006: initobj AssemblyTest.MyValType L_000c: ldarg.0 L_000d: call instance void [mscorlib]System.Object::.ctor() L_0012: nop L_0013: ret }.field private valuetype AssemblyTest.MyValType MyVal1.field private valuetype AssemblyTest.MyValType MyVal2 } … 0000 Reference type Value type

Exceptional Cases Case 2: Multiple initializations for the same object Case 2: Multiple initializations for the same object – = 2… Case 3: Initializers can’t get exception Case 3: Initializers can’t get exception – Same objects maybe get exception during Constructor time

Summary 請謹慎選用 Initializers 請謹慎選用 Initializers – 此 Item 雖然很豪邁的叫你直接用 Initializers ,但馬上講了三個例 外。

Initialize Static Class Members with Static Constructors Item 13:

Why we don’t initialize it in constructor? The static member will be reset value every construct time The static member will be reset value every construct time – Singleton pattern is important for static member

How to initialize Static Class Members ? Use Initializers Use Initializers – Singleton pattern Use Static Constructors Use Static Constructors – Singleton pattern – More flexibility – Error recovery here

Summary Static Constructors 與 Initializers 對於初始化 Static Class Members 並沒有優劣之分 Static Constructors 與 Initializers 對於初始化 Static Class Members 並沒有優劣之分 請依據適合的情況使用它們 請依據適合的情況使用它們