Professional Documents
Culture Documents
1
Objectives
2
Declaring an Enumeration Type
Variables of this new type can be used just like built-in types.
3
Using an Enumeration Type
Declare a variable of
Season Time_of_Year; type Season
...
Set the variable to a
Time_of_Year = Season.Summer; value of the enumeration
4
enum Example
using System;
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();
}
}
5
Using an Enumeration Type
Why do this?
Like enums in C
Except: typesafe
6
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.");
}
}
Note typecasts
8
Enums are not ints
9
Enumeration Values
11
Names of Enumeration Values
12
The ToString() Function
13
Enumeration Values
Any value not specified gets the next higher integer value. 14
Enumeration Values
End of Section
15
struct Types
16
struct Types
17
struct Example
structs in C#
Unlike C,
C# permits structs to have methods and
constructors.
Value Types
Allocated on the stack rather than on the heap
local variables
parameters of methods
20
structs in C#
Other differences from classes:
You cannot write a default constructor.
The compiler always provides one.
Initializes members to 0, false, null
21
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;
}
}
22
Constructors for structs
This gets a compile error:
struct Time
{
public int hours;
public int minutes; // Never initialized
public int seconds; // Never initialized
23
Differences from Classes
25
Default Construct for Struct
This works:
static void Main(string[ ] args)
{
Time Quitting_Time = new Time(); // default constructor
Quitting_Time.hours = 6;
Console.WriteLine(Quitting_Time.hours);
}
26
Assigning Structs
The RHS data is copied to the LHS fields with a fast single
block copy that never throws an exception.
RHS must be initialized. 27
Comparing Structs
Operators == and != are not automatically defined. (like C)
You can provide your own implementation of these operators (Ch. 21)
28
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.
29
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.
30
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().
31
Assignment
Read Chapter 9.
Try some examples.
End of Presentation
32