1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define...
-
Upload
brian-merritt -
Category
Documents
-
view
217 -
download
1
Transcript of 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define...
![Page 1: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/1.jpg)
1
Enumerations and Structs
Chapter 9
![Page 2: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/2.jpg)
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.
![Page 3: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/3.jpg)
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)
![Page 4: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/4.jpg)
4
Using an Enumeration Type
Season Time_of_Year;
...
Time_of_Year = Season.Summer;
Declare a variable of type Season
Set the variable to a value of the enumeration
![Page 5: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/5.jpg)
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();
}
}
![Page 6: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/6.jpg)
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.
![Page 7: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/7.jpg)
Comparing enumsstatic 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();}
![Page 8: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/8.jpg)
8
Enumeration Values
By default, the compiler assigns sequential values starting with 0.
static void Main(string[ ] args){
Console.WriteLine( (int) Season.Spring); // Outputs 0Console.WriteLine( (int) Season.Summer); // Outputs 1Console.WriteLine( (int) Season.Fall); // Outputs 2Console.WriteLine( (int) Season.Winter); // OUtputs 3
}
Note typecasts
![Page 9: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/9.jpg)
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.
![Page 10: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/10.jpg)
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();
}
![Page 11: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/11.jpg)
11
Enumeration Values
The system knows the names of the values.
Console.WriteLine(Season.Spring); // Outputs SpringConsole.WriteLine(Season.Summer); // Outputs SummerConsole.WriteLine(Season.Fall); // Outputs FallConsole.WriteLine(Season.Winter); // Outputs Winter
Console.WriteLine((Season) 0); // Outputs SpringConsole.WriteLine((Season) 1); // Outputs SummerConsole.WriteLine((Season) 2); // Outputs FallConsole.WriteLine((Season) 3); // Outputs Winter
![Page 12: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/12.jpg)
12
Names of Enumeration Values
Every enumeration type automatically has a built-in “ToString()” functionenum Season { Spring, Summer, Fall, Winter }
static void Main(string[] args){ Season baseball = Season.Summer; Console.WriteLine (baseball.ToString()); // Outputs Summer}
![Page 13: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/13.jpg)
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
}
![Page 14: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/14.jpg)
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 3Console.WriteLine((int) Season.Summer); // Outputs 5Console.WriteLine((int) Season.Fall); // Outputs 7Console.WriteLine((int) Season.Winter); // Outputs 8
}
Any value not specified gets the next higher integer value.
![Page 15: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/15.jpg)
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 0Console.WriteLine((int) Season.Summer); // Outputs 1Console.WriteLine((int) Season.Fall); // Outputs 2Console.WriteLine((int) Season.Autum); // Outputs 2Console.WriteLine((int) Season.Winter); // Outputs 3
}
End of Section
![Page 16: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/16.jpg)
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.
![Page 17: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/17.jpg)
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;
}
![Page 18: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/18.jpg)
struct Example
![Page 19: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/19.jpg)
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.
![Page 20: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/20.jpg)
20
structs in C#
Value TypesAllocated on the stack rather than on the heap
local variables parameters of methods
Variables represent the actual dataNot a pointer to the data
Assignment copies the data.Does not make the RHS variable an alias for the LHS variable.
![Page 21: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/21.jpg)
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.
![Page 22: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/22.jpg)
22
Constructors for structs
This compiles:struct Time{
public int hours; public int minutes; public int seconds;
// Constructorpublic Time (int h, int m, int s){
hours = h; minutes = m; seconds = s;
}}
![Page 23: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/23.jpg)
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;}
}
![Page 24: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/24.jpg)
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.)
![Page 25: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/25.jpg)
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 onlyQuitting_Time.hours = 6;Console.WriteLine (Quitting_Time.hours);
}
If Time had been a class, this would not compile.
![Page 26: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/26.jpg)
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 constructorQuitting_Time.hours = 6;
Console.WriteLine(Quitting_Time.hours);}
This would also work if Time had been a class.
![Page 27: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/27.jpg)
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.
![Page 28: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/28.jpg)
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
![Page 29: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/29.jpg)
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.
![Page 30: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/30.jpg)
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.
![Page 31: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/31.jpg)
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().
![Page 32: 1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.](https://reader036.fdocuments.in/reader036/viewer/2022062408/56649f325503460f94c4ea09/html5/thumbnails/32.jpg)
32
Assignment
Read Chapter 9.
Try some examples.
End of Presentation