Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Effective C# 50 Specific Way to Improve Your C# Item 6, 7 Sephiroth.Wang2012/08/01."— Presentation transcript:

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

2 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

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

4 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

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

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

7 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

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

9 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?

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

11 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

12 // 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. }

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

14 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", 61111 ); // To change, re-initialize: a1 = new Address( a1.Line1, a1.Line2, "Ann Arbor", "MI", 48103 );

15 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); }... }

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


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

Similar presentations


Ads by Google