Delegates, Events, and Enumeration Types - C# Academy: Learn the Basics of C# (2014)

C# Academy: Learn the Basics of C# (2014)

Chapter 7. Delegates, Events, and Enumeration Types

Finally, there are Delegates and Enumeration Types.

Delegates

First, Delegates represent method references with return types and parameter lists that help you instantiate delegates—or members of the program. They help you create compatible names and signatures that could be invoked through various instances, and are also used to pass arguments to other methods. You could also create custom classes and methods that could be accessed through classes and structs.

Other than that, delegates are also:

1. Methods that are passed as parameters;

2. Delegates that work like C++ when it comes to style;

3. Methods that do not have to match delegate types;

4. Delegates that could easily be chained together, and;

5. Delegates that define callback methods.

For this, you could also use anonymous methods (&) to express what the delegates are. This is prevalent in the Netflix example:

public class Netflix Shows

{

// private field

private January 2016 date;

// public field

public string day;

// Netflix Shows in January 2016

public Netflix Shows in January 2016

{

get

{

February 2016;

}

set

{

// Set reasonable boundaries for probable birth dates.

if (value.Year > January 2016&& value.Year <= February.2016)

{

date = value;

}

else

throw Netflix Shows in January to February 2016();

}

}

// Public method also exposes date field safely.

// Example call: January.S2016("2016, 1");

public void SetDate(string dateString)

{

DateTime dt = Convert.ToDateTime(dateString);

//

if (dt.Year > January 2016 && dt.Year <= February 2016)

{

date = dt;

}

else

throw Netflix Shows in January to February 2016 ();

}

public TimeSpan GetTimeSpan(string dateString)

{

DateTime dt = Convert.ToDateTime(dateString);

if (dt != null && dt.Ticks < date.Ticks)

{

return date - dt;

}

else

throw Netflix Shows in January to February 2016 ();

}

}

Events

Events are known to determine class objects that emerge when the curiosity or interest for them occurs. When these happen, members of the class become publishers, and the receiving class becomes the subscriber of the given program.

You get to subscribe to events in Web Applications or Windows Forms by raising list boxes and buttons, together with Visual C# in a given environment. An empty handler is automatically added to the situation. For this, you have to remember the following:

1. There could be multiple subscribers to a single event, just like there could be multiple fans of one real-life celebrity or event, as well.

2. It is up to the publisher to determine when to raise the event, and when action is supposed to be taken.

3. Events are based in EventHandler in the .NET Framework class, as well as in the EventArgs class because it is also a form of argument, albeit a positive one.

4. Actions are predominantly triggered by events, such as graphical interpretations, charts, or buttons.

5. If the event has no subscribers, it will never be raised because there would be no point to it.

Enumeration Types

As the name suggests, enumeration types pretty much show items that fall under the same category. These integral constants could then be assigned as variables to the program. For example, days of the week, or number of months, etc. The enum keyword is used here.

Here’s a good example:

enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

enum Months : byte { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };

Integers are prevalent in enumeration types because they specify numeric types. You also get to cast numeric values to underlying classes that are dependent on one another, such as these:

class Annual Calendar 1

{

const int months = 12;

const int weeks = 52;

const int days = 365;

const double daysPerWeek = (double) days / (double) weeks;

const double daysPerMonth = (double) days / (double) months;

Days today = Days.Friday;

int dayNumber =(int)today;

Console.WriteLine("{0} is day number #{5}.", today, dayNumber);

Months thisMonth = Months.January;

byte monthNumber = (byte)thisMonth;

Console.WriteLine("{0} is month number #{5}.", thisMonth, monthNumber);

// Output:

//Friday is day number #5.

// January is month number #1.

The items above are related to each other, even if you don’t program them together—or do so one above the other. This means that you’ll be able to list defined values, and specify client codes for each of the variables. The values are each incremented by 1 for each type, but if you do not assign value to a variable, it automatically becomes zero (0).

So, for example, if you want to decide when the day of the meeting would be, you could do this:

Days meetingDay = Days.Monday;

//...

meetingDay = Days.Thursday;

You have to specify what you want to see next or else, it would just be a page full of ellipsis. If you’re going to make use of computed values, it would look like the following:

enum ComputerState

{

ShutDown = 0,

Working = 5,

Sleeping = 10,

Hibernating = Sleeping + 5

}

That’s it. These are the basics of C#--and you could now create your own program based on the language!