Top Banner
Constants
23

Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

Constants

Page 2: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

2

Objectives

• Describe ways to create constants– const– readonly– enum

Page 3: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

3

Motivation

• Idea of constant is useful– makes programs more readable– allows more compile time error checking

Page 4: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

4

Const

• Keyword const used to indicate compile time constant– applicable to local variables– applicable to fields

Page 5: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

5

Const local variable

• Local variable can be declared constant– must be initialized inline– value cannot be changed later

double Area(double radius){ const double pi = 3.14;

return pi * radius * radius;}

constantlocal variable

Page 6: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

6

Const field

• Field can be declared constant– must be initialized inline– value cannot be changed later

public class Math{ public const double PI = 3.14159265358979323846; public const double E = 2.7182818284590452354; ...}

constantfields

Page 7: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

7

Const and static

• Field cannot be both declared both const and static– not needed– const field automatically static– saves memory by generating single copy

public class Math{ public const double PI = 3.14159265358979323846; public const double E = 2.7182818284590452354; ...}

implicitlystatic

double radius = 1.5;

double area = Math.PI * radius * radius; access usingtype name

Page 8: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

8

Compile time const

• const provides restrictive notion of constant– value must be computable at compile time

void Process(int width, int height){ const int area = width * height; ...}

error, value must becompile time constant

Page 9: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

9

Limited const references

• Value for const reference must be compile time constant– references can only be null– too limited to be truly useful

• Works for strings since compile time literals exist

const Person p = null;

const Person ann = new Person("Ann");

ok

error

const string greeting = "hello";ok

Page 10: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

10

No const parameters

• Parameters cannot be const– not value type parameters– not reference type parameters– parameter value is not determined at compile time

double Process(const Person p){ ...}

error, constparametersnot supported

double Average(const int a, const int b){ ...}

error, constparametersnot supported

Page 11: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

11

Readonly

• Keyword readonly used to indicate runtime constant– applicable only to fields

Page 12: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

12

Readonly field

• Field can be declared readonly– can only be initialized using variable initializer or in constructor– value cannot be changed later– compiler warning if not set– value used for initialization can be determined at runtime

class Person{ readonly DateTime dob;

public Person(DateTime dob) { this.dob = dob; } ...}

readonly field

Page 13: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

13

ok

Readonly and static

• Fields often made both readonly and static– more flexible than const– prevents write access– allows runtime determination of value– saves memory by generating single copy

struct Point{ public static readonly Point origin = new Point(0, 0); ...}

Page 14: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

14

Enum

• Keyword enum used to create new type– with corresponding set of symbolic constants

Page 15: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

15

Enum definition

• Can create enumeration value type– use keyword enum– specify type name– give list of named constants

enum Day{ Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}

define enum

Page 16: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

16

Enum use

• Can create variables of enum type– use enum name as type name

• Can use values listed in enum– values are scoped inside enum– prefix with enum name for access

Day d;

d = Day.Monday;

if (d == Day.Saturday || d == Day.Sunday) Console.WriteLine("weekend");

create variable

Page 17: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

17

Enum underlying type

• Enum uses an underlying type for representation– default is int– can use any integral type except char

representedusing int

enum ErrorCode : byte{ DomainError, RangeError }

representedusing short

enum Day{ Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}

Page 18: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

18

Enum values

• Enum constants have values– default is sequential starting at 0– can explicitly specify any value and others follow in sequence

enum Day{ Sunday, // 0 Monday, // 1 Tuesday, // 2 Wednesday, // 3 Thursday, // 4 Friday, // 5 Saturday // 6}

defaultvalues

enum Day{ Sunday = 1, Monday, // 2 Tuesday, // 3 Wednesday, // 4 Thursday = 10, Friday, // 11 Saturday // 12}

specifyvalue

Page 19: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

19

Enum and casting

• Must cast to convert between enum and underlying type– cast needed in either direction– helps reduce chance of assigning invalid value

Day d = (Day)3;

int i = (int)d;

convert to Day

convert to int

Page 20: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

20

Enum inheritance hierarchy

• enum is part of type hierarchy – ultimately descends from Object like all types– automatically derived from the value type Enum– Enum class provides many convenient utility methods

ValueType

Object

Enum

user enum

Page 21: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

21

Boxing

• Enums are value types– boxed when used as object– value copied into box

Day d = Day.Monday;

object o = d;...

boxed

o Monday

Mondayd

Page 22: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

22

Unboxing

• Can extract enum from box– cast required– System.InvalidCastException thrown if cast fails

Day d = Day.Monday;object o = d;

Day e = (Day)o;...

unbox

o Monday

Mondaye

Page 23: Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.

23

Summary

• Three different techniques to create constants– const– readonly– enum

• static often combined with readonly on field– gives more flexible notion of constant than const keyword