Effective C# 50 Specific Way to Improve Your C# Item 6, 7 Sephiroth.Wang2012/08/01.

Slides:



Advertisements
Similar presentations
Introduction to Java Programming Lecture 10 Method Benefits, Declaring, and Calling Methods.
Advertisements

Chapter Programming in C
1 生物計算期末作業 暨南大學資訊工程系 2003/05/13. 2 compare f1 f2  只比較兩個檔案 f1 與 f2 ,比完後將結果輸出。 compare directory  以兩兩比對的方式,比對一個目錄下所有檔案的相 似程度。  將相似度很高的檔案做成報表輸出,報表中至少要.
Introduction to Java Programming Lecture 13 Classes I OO Programming.
第七章 抽樣與抽樣分配 蒐集統計資料最常見的方式是抽查。這 牽涉到兩個問題: 抽出的樣本是否具有代表性?是否能反應出母體的特徵?
“Rule” By OX. By Check CREATE TABLE 員工薪資 ( 編號 int IDENTITY PRIMARY KEY, 薪資 smallmoney, CHECK ( 薪資 > 0 AND 薪資
PowerPoint2010 李燕秋 版面配置 版面配置指的是每一個頁面的內容配置 方式,不同的版面配置會有對應的母片。
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章.
Introduction to Constructor, Overloading, and Accessibility CS340100, NTHU Yoshi.
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章 CS340100, NTHU Yoshi.
亂數產生器安全性評估 之統計測試 SEC HW7 姓名:翁玉芬 學號:
近接開關 高頻型近接開關 高頻型近接開關 電容型近接開關 電容型近接開關 磁力型近接開關 磁力型近接開關.
建立靜態資料成員 關鍵字: static 靜態資料成員不屬於特定物件,而是該類物件 所共同擁有的,可做為類別範圍裡的靜態變數。
第一章 變數、常數及資料型態. 變數 C 程式語言的變數名稱 第一個字必須是英文字母或底線 (_) 之後可以是數字, 英文字母或底線 (_) 不可以是保留字 例: Num (Ο) _score (Ο) C&C (X) 8num (X)
1 政治大學公企中心必修課 -- 社會科學研究方法(量化分析) -- 黃智聰 政治大學公企中心必修課 課程名稱:社會科學研究方法(量化分析) 授課老師:黃智聰 授課內容: 質化的及有限的因變數模型 參考書目: Hill, C. R., W. E. Griffiths, and G. G. Judge,
STAT0_sampling Random Sampling  母體: Finite population & Infinity population  由一大小為 N 的有限母體中抽出一樣本數為 n 的樣 本,若每一樣本被抽出的機率是一樣的,這樣本稱 為隨機樣本 (random sample)
第 13 章 常數、靜態成員與朋友關 係 13-1 常數物件與成員 13-1 常數物件與成員 13-2 物件成員與巢狀類別 13-2 物件成員與巢狀類別 13-3 靜態成員資料與函數 13-3 靜態成員資料與函數 13-4 朋友關係 13-4 朋友關係.
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
例外處理. 內 容 大 綱 例外的產生 捕捉例外 丟出例外 例外的產生 (1/4) 一般常見的執行時期錯誤包括整數除法分母為 0(divide by zero) ,陣列索引值越限 (array index out of range) ,輸入輸出錯誤 (I/O error) 、檔案 不存在或無法存取.
第 18 章 名稱空間與例外處理 18-1 名稱空間 18-1 名稱空間 18-2 例外處理 18-2 例外處理.
系統分析與設計【第八版】 第六章 物件塑模.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
Monte Carlo Simulation Part.2 Metropolis Algorithm Dept. Phys. Tunghai Univ. Numerical Methods C. T. Shih.
1 Part IC. Descriptive Statistics Multivariate Statistics ( 多變量統計 ) Focus: Multiple Regression ( 多元迴歸、複迴歸 ) Spring 2007.
2009fallStat_samplec.i.1 Chap10 Sampling distribution (review) 樣本必須是隨機樣本 (random sample) ,才能代表母體 Sample mean 是一隨機變數,隨著每一次抽出來的 樣本值不同,它的值也不同,但會有規律性 為了要知道估計的精確性,必需要知道樣本平均數.
Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009.
Chapter 13 塑模靜態觀點:物件圖 Static View : Object Diagram.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
1 Introduction to Chemical Engineering Thermodynamics Residual Gibbs free energy of fluids Smith.
Introduction to Java Programming Lecture 10 Array I Declaring, Creating, and Initializing Arrays.
Matlab Assignment Due Assignment 兩個 matlab 程式 : Eigenface : Eigenvector 和 eigenvalue 的應用. Fractal : Affine transform( rotation, translation,
Hung-Hsiang WuWindows Processing Design1 Chapter 3 基本觀念 變數宣告與型態 特殊運算子符號 字串與數值的轉換 類別與物件的觀念 建立新的專案 WinMain 程式進入點 Include Header File.
: Happy Number ★ ? 題組: Problem Set Archive with Online Judge 題號: 10591: Happy Number 解題者:陳瀅文 解題日期: 2006 年 6 月 6 日 題意:判斷一個正整數 N 是否為 Happy Number.
Chapter 12 Pointers and Dynamic Arrays. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Pointers (12.1) Dynamic Arrays.
: Playing War ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11061: Playing War 解題者:陳盈村 解題日期: 2008 年 3 月 14 日 題意:在此遊戲中,有一類玩家一旦開始攻擊, 就會不停攻擊同一對手,直到全滅對方或無法再.
資料庫程式設計與系統管理 SQL Server 2005 Express 第六章 進階資料庫設計.
:Nuts for nuts..Nuts for nuts.. ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10944:Nuts for nuts.. 解題者:楊家豪 解題日期: 2006 年 2 月 題意: 給定兩個正整數 x,y.
資料結構實習-一 參數傳遞.
Dynamic Multi-signatures for Secure Autonomous Agents Panayiotis Kotzanikolaou Mike Burmester.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
Section 4.2 Probability Models 機率模式. 由實驗看機率 實驗前先列出所有可能的實驗結果。 – 擲銅板:正面或反面。 – 擲骰子: 1~6 點。 – 擲骰子兩顆: (1,1),(1,2),(1,3),… 等 36 種。 決定每一個可能的實驗結果發生機率。 – 實驗後所有的實驗結果整理得到。
Teacher : Ing-Jer Huang TA : Chien-Hung Chen 2015/6/25 Course Embedded Systems : Principles and Implementations Weekly Preview Question CH 2.4~CH 2.6 &
函式 Function Part.2 東海大學物理系‧資訊教育 施奇廷. 遞迴( Recursion ) 函式可以「呼叫自己」,這種動作稱為 「遞迴」 此程式的執行結果相當於陷入無窮迴圈, 無法停止(只能按 Ctrl-C ) 這給我們一個暗示:函式的遞迴呼叫可以 達到部分迴圈的效果.
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
: Flip Sort ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10327: Flip Sort 解題者:歐子揚 解題日期: 2010 年 2 月 26 日 題意:在這個問題中使用一種排序方式 (Flip) ,意思就是 只能交換相鄰的.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
Linguistics phonetic symbols. 先下載 IPA 字型檔案,執行安裝。 由於這個程式的字型目錄設定錯誤, 所以等重新開機時就會發現字型消失。 所以必須根據以下步驟來讓 Windows 加入 IPA 字型。
Structural Equation Modeling Chapter 6 CFA 根據每個因素有多重指標,以減少 測量誤差並可建立問卷的構念效度 驗證性因素分析.
Chapter 10 m-way 搜尋樹與B-Tree
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
JAVA 程式設計與資料結構 第十六章 Hash Tables. Introduction Hash Tables 結構為一個 Array ,稱之為 Bucket array 。 如果想要新增一個物件,要根據這個物件的特性 將其加入 Hash Table 內。 Bucket Array 用 A 來代替,其.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
第 6 章 迴圈結構 6-1 計數迴圈 6-1 計數迴圈 6-2 條件迴圈 6-2 條件迴圈 6-3 巢狀迴圈 6-3 巢狀迴圈 6-4 While/End While 迴圈 6-4 While/End While 迴圈 6-5 跳出迴圈 6-5 跳出迴圈 6-6 VB.NET 的錯誤處理 6-6 VB.NET.
Teacher : Ing-Jer Huang TA : Chien-Hung Chen 2015/6/30 Course Embedded Systems : Principles and Implementations Weekly Preview Question CH7.1~CH /12/26.
:Commandos ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11463: Commandos 解題者:李重儀 解題日期: 2008 年 8 月 11 日 題意: 題目會給你一個敵營區內總共的建築物數,以及建築物 之間可以互通的路有哪些,並給你起點的建築物和終點.
Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods.
函式 Function 東海大學物理系‧資訊教育 施奇廷. 函式簡介 當程式越來越大、越複雜時,程式的維護、 除錯會變得更困難,此時必須引入函式來 簡化程式或將程式分段,將程式重複的部 分改寫為函式,將程式「模組化」 這種作法有下列優點:節省程式發展的時 間、邏輯容易瞭解、程式容易除錯、可分 工合作完成程式.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;
幼兒行為觀察與記錄 第八章 事件取樣法.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
物件導向實習 極高的忘記答題率 … AB 卷都有的題目 : 4(1). Define method overloading 明明寫出了方法, 卻不回答老師的題目 (1)( 只要 寫出定義就好了 ) 另外, 讀題一定要仔細 : Two overloaded methods average.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
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.
Software Engineering for Digital Home 單元 3 :軟體設計 Chapter 3-6 – Specifying Interfaces.
Sun Educational Service 第九單元 實作封裝與建構式. Sun Educational Service Module 9, slide 2 of 43 單元概論 單元目標: – 利用封裝 (encapsulation) 來保護資料 – 設計建構式來初始化物件 問題與討論.
Speaker: Liu Yu-Jiun Date: 2009/4/15
Presentation transcript:

Effective C# 50 Specific Way to Improve Your C# Item 6, 7 Sephiroth.Wang2012/08/01

Agenda Item 6: Distinguish Between Value Types and Reference Types Item 6: Distinguish Between Value Types and Reference Types Item 7: Prefer Immutable Atomic Value Types Item 7: Prefer Immutable Atomic Value Types

Item 6: Distinguish Between Value Types and Reference Types Item 6: Distinguish Between Value Types and Reference Types

What is Value Types and Reference Type Value TypeReference Type Polymorphic(  )Polymorphic ( ) More efficientLess efficient, data consistent C++JAVA Store in stackStore in heap 1.Simple types (sbyte, short, int, long, char, float, double, decimal, bool,) 2.Enum types 3.Struct types 1.Class types 2.Interface types 3.Array types 4.Delegate types

Value Type and Reference Type private MyData _myData; public MyData Foo() { return _myData; } // call it: MyData v = Foo(); TotalSum += v.Value; private MyData _myData; public MyData Foo() { return _myData.Clone( ) as MyData; } // call it: MyData v = Foo(); TotalSum += v.Value;

Value Type and Reference Type private MyType _myType; public IMyInterface Foo() { return _myType as IMyInterface; } // call it: IMyInterface iMe = Foo(); iMe.DoWork( );

Size public class C { private MyType _a = new MyType( ); private MyType _b = new MyType( ); // Remaining implementation removed. } C var = new C(); MyType [] var = new MyType[ 100 ]; If (Value type) one allocation, but size is double Else if (Reference type) Three allocation 1. Object C 2. MyTyep a, and b

Example public struct Employee { private string _name; private int _ID; private decimal _salary; // Properties elided public void Pay( BankAccount b ) { b.Balance += _salary; } public class Employee { private string _name; private int _ID; private decimal _salary; // Properties elided public virtual void Pay( BankAccount b ) { b.Balance += _salary; } Employee e1 = Employees.Find( "CEO" ); e1.Salary += Bonus; // Add one time bonus. e1.Pay( CEOBankAccount );

Summary Value type 比較有好的效率,記憶體比較不會有 Fragment 。 Value type 是用複製物件方式,避免 Member 被修改到。 Value type 比較有好的效率,記憶體比較不會有 Fragment 。 Value type 是用複製物件方式,避免 Member 被修改到。 Is this type's principal responsibility data storage? Is this type's principal responsibility data storage? Is its public interface defined entirely by properties that access or modify its data members? Is its public interface defined entirely by properties that access or modify its data members? Am I confident that this type will never have subclasses? Am I confident that this type will never have subclasses? Am I confident that this type will never be treated polymorphically? Am I confident that this type will never be treated polymorphically?

Item 7: Prefer Immutable Atomic Value Types Item 7: Prefer Immutable Atomic Value Types

Immutable Atomic Value Types Advantage: 1. 常量性類型由於建構後值就固定不變,因此只需在建構子做參數 的檢查,可省略許多必要的錯誤檢查。 1. 常量性類型由於建構後值就固定不變,因此只需在建構子做參數 的檢查,可省略許多必要的錯誤檢查。 2. Immutable types are inherently thread safe 2. Immutable types are inherently thread safe 3. The caller cannot modify the internal state of your objects. 3. The caller cannot modify the internal state of your objects. 4. The value returned by Object.GetHashCode() must be an instance invariant 4. The value returned by Object.GetHashCode() must be an instance invariant

// Mutable Address structure. public struct Address { private string _line1; private string _line2; private string _city; private string _state; private int _zipCode; // Rely on the default system-generated // constructor. public string Line1 { get { return _line1; } set { _line1 = value; } } public string Line2 { get { return _line2; } set { _line2 = value; } } public string City { get { return _city; } set { _city= value; } } public string State { get { return _state; } set { ValidateState(value); _state = value; } public int ZipCode { get { return _zipCode; } set { ValidateZip( value ); _zipCode = value; } // other details omitted. }

// Example usage: Address a1 = new Address( ); a1.Line1 = "111 S. Main"; a1.City = "Anytown"; a1.State = "IL"; a1.ZipCode = ; // Modify: a1.City = "Ann Arbor"; // Zip, State invalid now. a1.ZipCode = 48103; // State still invalid now. a1.State = “MI”; // Now fine.

public struct Address { private readonly String _line; private readonly String _city; private readonly String _state; private readonly int _zipCode; public string Line { get { return _line; } } public string City { get { return _city; } } public string State { get { return _state; } } public int ZipCode { get { return _zipCode; } } public Address(string line,string city,string state,int zipCode) { _line = line; _city = city; _state = state; _zipCode = zipCode; ValidateState(state); ValidateZip(zipCode); } // Create an address: Address a1 = new Address( "111 S. Main", "", "Anytown", "IL", ); // To change, re-initialize: a1 = new Address( a1.Line1, a1.Line2, "Ann Arbor", "MI", );

One hole that would allow clients to change your internal state public struct PhoneList { private readonly Phone[] _phones; public PhoneList(Phone[] ph) { _phones = ph; }... } Phone[] phones = new Phone[10]; PhoneList ps = new PhoneList(phones);... //Modify phones[5] = Phone.GeneratePhoneNumber(); public struct PhoneList { private readonly Phone[] _phones; public PhoneList(Phone[] ph) { _phones = new Phone[ph.Length]; ph.CopyTo(_phones,0); }... }

Summary 定義架構時,記得防範好每個可能發生的情況。避免資 料被修改後,不好 Debug 。 定義架構時,記得防範好每個可能發生的情況。避免資 料被修改後,不好 Debug 。