Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.

Similar presentations


Presentation on theme: "1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that."— Presentation transcript:

1 1 Enumerations and Structs Chapter 9

2 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that define and use struct variables. Describe the differences between structs and classes.

3 3 Declaring an Enumeration Type enum Season { Spring, Summer, Fall, Winter} Defines a new type with values Season.Spring Season.Summer Season.Fall Season.Winter Variables of this new type can be used just like built-in types. They are value types, like int. But not interchangable with int (Unlike C) Also not interchangable with other enum types (can typecast)

4 4 Using an Enumeration Type SeasonTime_of_Year;... Time_of_Year = Season.Summer; Declare a variable of type Season Set the variable to a value of the enumeration

5 5 enum Example using System; enum Season {Spring, Summer, Fall, Winter} class Program { static void Main(string[] args) { Season Time_of_Year; Time_of_Year = Season.Summer; Console.WriteLine("Time of year is " + Time_of_Year); Console.ReadLine(); }

6 6 Using an Enumeration Type Why do this? To provide meaningful names for a set of discrete values. Like enums in C Except: typesafe The compiler and the debugger understand.

7 Comparing enums static void Set_HVAC (Season Time_of_Year) { if (Time_of_Year == Season.Summer) { Console.WriteLine("Turn on cooling"); } else if (Time_of_Year == Season.Winter) { Console.WriteLine("Turn on heating"); } else { Console.WriteLine("Minimize engergy bill."); } } static void Main(string[ ] args) { Season Time_of_Year; Time_of_Year = Season.Summer; Console.WriteLine("Time of year is " + Time_of_Year); Set_HVAC(Time_of_Year); Console.ReadLine(); }

8 8 Enumeration Values By default, the compiler assigns sequential values starting with 0. static void Main(string[ ] args) { Console.WriteLine( (int) Season.Spring);// Outputs 0 Console.WriteLine( (int) Season.Summer);// Outputs 1 Console.WriteLine( (int) Season.Fall);// Outputs 2 Console.WriteLine( (int) Season.Winter);// OUtputs 3 } Note typecasts

9 9 Enums are not ints static void Main(string[] args) { Season Time_of_Year; Time_of_Year = Season.Winter; Console.WriteLine("Time of year is " + Time_of_Year); Set_HVAC(Time_of_Year); int i = Time_of_Year; Console.ReadLine(); } This gets a compile error.

10 10 Enumeration Values You can typecast in either direction. static void Main(string[] args) { Season Time_of_Year; Time_of_Year = Season.Winter; Console.WriteLine("Time of year is " + Time_of_Year); Set_HVAC(Time_of_Year); int i = (int) Time_of_Year; Console.WriteLine(i); Season Skiing = (Season) 3; Console.WriteLine(Skiing); Console.ReadLine(); }

11 11 Enumeration Values The system knows the names of the values. Console.WriteLine(Season.Spring);// Outputs Spring Console.WriteLine(Season.Summer);// Outputs Summer Console.WriteLine(Season.Fall);// Outputs Fall Console.WriteLine(Season.Winter);// Outputs Winter Console.WriteLine((Season) 0); // Outputs Spring Console.WriteLine((Season) 1); // Outputs Summer Console.WriteLine((Season) 2);// Outputs Fall Console.WriteLine((Season) 3); // Outputs Winter

12 12 Names of Enumeration Values Every enumeration type automatically has a built-in “ToString()” function enum Season { Spring, Summer, Fall, Winter } static void Main(string[] args) { Season baseball = Season.Summer; Console.WriteLine (baseball.ToString());// Outputs Summer }

13 13 The ToString() Function enum Season { Spring, Summer, Fall, Winter } static void Main(string[ ] args) { Season baseball = Season.Summer; string tempString = baseball.ToString(); Console.WriteLine(tempString); // Outputs Summer }

14 14 Enumeration Values You can control the integer value representing an enumeration value. enum Season { Spring = 3, Summer = 5, Fall = 7, Winter } static void Main(string[ ] args) { Console.WriteLine((int) Season.Spring);// Outputs 3 Console.WriteLine((int) Season.Summer);// Outputs 5 Console.WriteLine((int) Season.Fall);// Outputs 7 Console.WriteLine((int) Season.Winter);// Outputs 8 } Any value not specified gets the next higher integer value.

15 15 Enumeration Values Duplicated values are OK. enum Season { Spring, Summer, Fall, Autum = Fall, Winter } static void Main(string[ ] args) { Console.WriteLine((int) Season.Spring);// Outputs 0 Console.WriteLine((int) Season.Summer);// Outputs 1 Console.WriteLine((int) Season.Fall);// Outputs 2 Console.WriteLine((int) Season.Autum);// Outputs 2 Console.WriteLine((int) Season.Winter);// Outputs 3 } End of Section

16 16 struct Types You can define struct types in C# just as in C. struct Time { int hours; int minutes; int seconds; } But unlike C the fields of a struct are, by default, private. So there would be no way to access the fields of this struct.

17 17 struct Types To make the fields of a struct accessible, you must declare them public. struct Time { public int hours; public int minutes; public int seconds; }

18 struct Example

19 19 structs in C# Unlike C, C# permits structs to have methods and constructors. This means that structs in C# are functionally similar to classes. The major difference is that they are value types.

20 20 structs in C# Value Types Allocated on the stack rather than on the heap local variables parameters of methods Variables represent the actual data Not a pointer to the data Assignment copies the data. Does not make the RHS variable an alias for the LHS variable.

21 21 structs in C# Other differences from classes: You cannot write a default constructor. The compiler always provides one. Initializes members to 0, false, null If you do write a constructor, it must initialize all fields. There is no default initialization.

22 22 Constructors for structs This compiles: struct Time { public int hours; public int minutes; public int seconds; // Constructor public Time (int h, int m, int s) { hours = h; minutes = m; seconds = s; }

23 23 Constructors for structs This gets a compile error: struct Time { public int hours; public int minutes;// Never initialized public int seconds;// Never initialized public Time (int h) { hours = h; }

24 24 Differences from Classes Other differences from classes: In a class you can initialize a field in the declaration. public int hours = 0; In a struct you cannot do this. No inheritance for structs. (We will look at inheritance for classes in Chapter 12.)

25 25 Differences from Classes Because struct is a value type, space for its data is allocated by the declaration. Using the definition of struct Time that we saw earlier this compiles and works correctly: static void Main(string[ ] args) { Time Quitting_Time;// declaration only Quitting_Time.hours = 6; Console.WriteLine (Quitting_Time.hours); } If Time had been a class, this would not compile.

26 26 Default Construct for Struct We still have the option of calling the default constructor. This works: static void Main(string[ ] args) { Time Quitting_Time = new Time();// default constructor Quitting_Time.hours = 6; Console.WriteLine(Quitting_Time.hours); } This would also work if Time had been a class.

27 27 Assigning Structs You can assign structs just like you do ints. static void Main(string[] args) { Time t = new Time(1, 2, 3); t.hours = 6; Time t2 = t; Console.WriteLine(t2.hours); Console.ReadLine(); } The RHS data is copied to the LHS fields with a fast single block copy that never throws an exception. RHS must be initialized.

28 28 Comparing Structs Operators == and != are not automatically defined. (like C) static void Main(string[ ] args) { Time Quitting_Time; Time Starting_Time; Quitting_Time = new Time(6, 0, 0); Starting_Time = Quitting_Time; if (Quitting_Time == Starting_Time) { Console.WriteLine("Don't go to work."); } } You can provide your own implementation of these operators (Ch. 21) This gets a compile error

29 29 Structs vs. Classes So when do I use a struct and when do I use a class? The main decision is between reference type and value type. General guideline: Use a class for large amounts of data. Use a struct for small amounts.

30 30 structs in C# So when do I use a struct and when do I use a class? After we have looked at derived classes and inheritance, you will often want to use classes in order to take advantage of those features. In C#, structs are essentially a simplified, light-weight alternative to classes.

31 31 Type Equivalences In the.NET Framework, there are really only structs and classes. int is an alias for a system defined struct called Int32. Same for all other C# value types. This makes it easy to mix languages within the.NET framework. This is why numeric types can have methods, such as Parse().

32 32 Assignment Read Chapter 9. Try some examples. End of Presentation


Download ppt "1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that."

Similar presentations


Ads by Google