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

properties and basic generics
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Properties and Basic Generics

January 30, 2008

slide-2
SLIDE 2

1

Properties

2

Basic Generics

1/18

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 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

slide-11
SLIDE 11

When should I use a . . .

. . . public member? . . . property? . . . method?

8/18

slide-12
SLIDE 12

When should I use a . . .

. . . public member? Only in trivial situations. Public members are not robust against design changes. . . . property? . . . method?

8/18

slide-13
SLIDE 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

slide-14
SLIDE 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

slide-15
SLIDE 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

slide-16
SLIDE 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

  • nly

patterns are considered bad s t y l e public i n t Y { set { myY = value ; } } }

10/18

slide-17
SLIDE 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

slide-18
SLIDE 18

1

Properties

2

Basic Generics

12/18

slide-19
SLIDE 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

slide-20
SLIDE 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

slide-21
SLIDE 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

slide-22
SLIDE 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

slide-23
SLIDE 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

slide-24
SLIDE 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

  • bjects.

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