properties and basic generics
play

Properties and Basic Generics January 30, 2008 Properties 1 Basic - PowerPoint PPT Presentation

Properties and Basic Generics January 30, 2008 Properties 1 Basic Generics 2 1/18 Recall last lecture: / / Create a form ( i . e . a window ) Form theForm = new Form ( ) ; / / Set the t i t l e theForm . Text = "My Window" ;


  1. Properties and Basic Generics January 30, 2008

  2. Properties 1 Basic Generics 2 1/18

  3. Recall last lecture: / / Create a form ( i . e . a window ) Form theForm = new Form ( ) ; / / Set the t i t l e theForm . Text = "My Window" ; 2/18

  4. Recall last lecture: / / Create a form ( i . e . a window ) Form theForm = new Form ( ) ; / / Set the t i t l e theForm . Text = "My Window" ; Q. Is theForm.Text really a member? 2/18

  5. Recall last lecture: / / Create a form ( i . e . a window ) Form theForm = new Form ( ) ; / / Set the t i t l e theForm . Text = "My Window" ; Q. Is theForm.Text really a member? A. No. theForm.Text is a property . 2/18

  6. Properties provide special syntax for methods. A property consists of two methods: get and set. Clients call set with assignment notation e.g. theForm.Text = "My Window"; Clients call get with member read notation e.g. WriteLine(theForm.Text) Each property access runs a method. 3/18

  7. Property Example (I/III) public class Temperature { p r i v at e double myKelvin ; public double Kelvin { get { / / Think " public double get ( ) " return myKelvin ; } set { / / Think " public void set ( double value ) " myKelvin = value ; } } . . . 4/18

  8. Property Example (II/III) . . . public double Fahrenheit { get { return myKelvin ∗ ( 9 . 0 / 5 . 0 ) − 459.67; } set { myKelvin = ( 5 . 0 / 9 . 0 ) ∗ ( value + 459.67); } } } 5/18

  9. Property Example (III/III) public class Runner { public s t a t i c void Main ( s t r i n g [ ] args ) { Temperature Temp = new Temperature ( ) ; Temp. Fahrenheit = 32.0; Console . Out . WriteLine (Temp. Kelvin ) ; } } Output : 273.15 ( that ’ s the r i g h t answer ) 6/18

  10. C# 3.0 has special syntax for declaring simple properties. public class Temperature { / / Compiler automatically generates p r i v at e / / member, getter , and s e t t e r public double Kelvin { get ; set ; } public double Fahrenheit { get { return Kelvin ∗ ( 9 . 0 / 5 . 0 ) − 459.67; } set { Kelvin = ( 5 . 0 / 9 . 0 ) ∗ ( value + 459.67); } } } 7/18

  11. When should I use a . . . . . . public member? . . . property? . . . method? 8/18

  12. When should I use a . . . . . . public member? Only in trivial situations. Public members are not robust against design changes. . . . property? . . . method? 8/18

  13. When should I use a . . . . . . public member? Only in trivial situations. Public members are not robust against design changes. . . . property? The getter has no (observable) side effects. The getter does not throw exceptions. Both get and set return almost immediately (no long computations or database queries) . . . method? 8/18

  14. When should I use a . . . . . . public member? Only in trivial situations. Public members are not robust against design changes. . . . property? The getter has no (observable) side effects. The getter does not throw exceptions. Both get and set return almost immediately (no long computations or database queries) . . . method? Any other time. 8/18

  15. Access limited properties. public class Misc { i n t myNumber ; / / A property with a p r i v at e getter . Only / / members of Misc can read . DropBox public i n t DropBox { set { myNumber = value ; } p r i v at e get { return myNumber ; } } public i n t PrivateSet { get ; p r i v at e set ; } } 9/18

  16. Read-only and write-only properties public class GetSetOnly { p r i v at e i n t myX, myY; / / A read − only property : a common pattern public i n t X { get { return myX; } } / / Write only patterns are considered bad s t y l e public i n t Y { set { myY = value ; } } } 10/18

  17. Technical notes about properties Properties compile to method calls, not member access So properties can’t implement members in interfaces Properties are optimized to be roughly as fast as member access 11/18

  18. Properties 1 Basic Generics 2 12/18

  19. Generics allow types (e.g. classes and delegates) to be parameterized by types. Provide extra compile-time type information Provide opportunities for compiler optimizations. Allow the compiler to catch bugs that would otherwise happen at runtime. Enhance code readability. Reduce need for downcasts (which are expensive and can throw exceptions). 13/18

  20. Example: a specialized “option” class. public class IntOption { p r i v at e bool i s F u l l ; p r i v at e i n t contents ; public bool Empty { get { return ! i s F u l l ; } } public i n t GetValue ( ) { i f ( i s F u l l ) return contents ; throw new Exception ( " GetValue of Empty" ) ; } public IntOption ( ) { i s F u l l = false ; } public IntOption ( i n t x ) { i s F u l l = true ; contents = x ; } } 14/18

  21. Example: Using the specialized option class. public class Runner { public s t a t i c IntOption div ( i n t x , i n t y ) { i f ( y==0) return new IntOption ( ) ; else return new IntOption ( x / y ) ; } public s t a t i c void Main ( s t r i n g [ ] args ) { Console . Out . WriteLine ( div ( 3 , 4 ) . Empty ) ; / / false Console . Out . WriteLine ( div ( 3 , 0 ) . Empty ) ; / / true } } 15/18

  22. Example: A generic option class. public class GenOption<T>{ p r i v at e bool i s F u l l ; p r i v at e T contents ; public bool Empty { get { return ! i s F u l l ; } } public T GetValue ( ) { i f ( i s F u l l ) return contents ; throw new Exception ( " GetValue of Empty" ) ; } public GenOption ( ) { i s F u l l = false ; } public GenOption (T x ) { i s F u l l = true ; contents = x ; } } 16/18

  23. Example: Using the generic option class. public class Runner { public s t a t i c GenOption< int > div ( i n t x , i n t y ) { i f ( y==0) return new GenOption< int > ( ) ; else return new GenOption< int >(x / y ) ; } public s t a t i c void Main ( s t r i n g [ ] args ) { Console . Out . WriteLine ( div ( 3 , 4 ) . Empty ) ; / / false Console . Out . WriteLine ( div ( 3 , 0 ) . Empty ) ; / / true } } 17/18

  24. Generics vs. Generics vs. Templates C++ Templates Template expansion is static: each template instantiation creates a new compile-time class. Templates can’t live in compiled libraries—only headers. Templates expansion = Turing complete programming language(!) Java Generics Similar semantics to C# Implemented by type erasure; no runtime support in JVM Poor support for reflection Legacy code can break apprent type guarantees for generic objects. C#Generics CLR (.Net virtual machine) has support for generics Generics can be specialized to used native types at runtime Type parameters preserved at runtime, and can be queried by reflection. 18/18

Recommend


More recommend