C# Workshop - Week 9 (Ch. 24 - 26)

Started by
1 comment, last by Telastyn 16 years, 5 months ago

Welcome to the GDNet C# Workshop – Ch. 24 - 26

For a complete introduction to this workshop, please look here. Workshop Overview This workshop is designed to aid people in their journey to learn beginning C# (C-Sharp). This workshop is targeted at highly motivated individuals who are interested in learning C# or who have attempted to learn C# in the past, but found that without sufficient support and mentoring they were unable to connect all the pieces of this highly complex but powerful programming language. This is a 'guided' self-teaching C# workshop. Each student is responsible for taking the time to read the material and learn the information. Additionally, this workshop does not attempt to defend C# as a language, nor does it attempt to demonstrate that C# is either more or less useful then other programming languages for any particular purpose. People who intend to start a discussion about the differences between C# and ANY other languages (except as are relevant to a particular discussion), are encouraged to do so elsewhere. This workshop is for educational, not philosophical discussions. Finally, the community and tutors that arise out of this workshop are here for making the learning process run more smoothly, but are not obligated to baby-sit a person's progress. Because everyone will be working from the same references (.NET Book Zero and optionally the C# Language Specification 1.2 & 2.0), students may find it easier to get answers to the specific questions they might have. There is no minimum age requirement, and there is no previous programming experience required. However, we will be moving quickly so it's essential that students stay on task and dont fall behind. Experienced C# Programmers Feel free to post your own additional knowledge about the topics, however please try and keep the information you provide objective. If you MUST provide subjective/opinion-based information, please do so by marking the paragraph with [opinion] tags. This will make it clear to the readers what is fact, and what is opinion. Also, it may be relevant to mark some information with [observation] tags for information which you’ve “observed” but may not be fact. Finally, if you’re providing information which is related to common programming errors, you might tag it with a [warning] tag. Also, feel free to post links to additional resources about the topics for this week. I will do my best to add those to the “Additional Resources” section at the bottom of this post. Quizzes & Exercises Each week I will post 'quiz' questions and exercises in the weekly thread. Please try and answer them by yourself. Once you've done so, feel free to look over the answers provided by others and submit your own answers if you've not yet seen them posted. Discussion about the quiz questions and answers is encouraged for clarification. Finally, experienced C# programmers may feel free to post quiz-like questions and exercises of their own.

Chapters 24 - 26

Introduction Welcome to Week 9 of the C# Workshop. With only two weeks of material left to go we dive into Events and Delegates, Files and Streams, and on String Theory. While the last 2 chapters of this week are more or less common to all programming languages, the concepts of Events and Delegates are one of the things that makes C# a truly brilliant language to work with. You see, one of the Design Patterns frequently dealt with is that of the Observer. This Observer design pattern describes how to define a one-to-many relationship between objects such that when one object changes its state, it can notify all of its listeners (observers) automatically. Even with only a single listener/observer this can be tedious, unnecessary code in other languages, and to get it to notify an entire list of registered listeners is even more work. Typically, implementing the Observer Pattern in other languages requires the creation of a base or templated class such as Notifier. This object then has methods which other objects use to register and remove themselves from the notifier's internal list of listeners. Additionally, the notifier frequently has 1 or more Notify or Update functions which iterates through it's internal list and calls a single method on all of it's listeners. As you might expect, this requires all classes who are listeners to implement an interface or derive from an Observer base class. So far we've had to create 2 base classes, a few methods, and internal list, and we're still limited to calling the methods provided by the Observer interface. The Observer Design Pattern is a powerful tool, but it's often quite a bit of overheard for very little flexibility. Fortunately, the C# Language does it all for us. In C#, we've got something referred to as an Event. An event is a member of a class which is of a delegate type and allows other classes or objects to register any listener to the event they like, so long as the listening method matches the return type and parameter list of the delegate used for the Event. Poof!, that's it. With a single event on the notifier, and an arbitrarily named method on the observer, a relationship can be created between the two classes simply by using the += operator on the event to register the method as a listener. Unregistering is as simple as using the -= operator. As a result of the Event construct being so simple to use, it's one of the core features of C# and what makes C# such a powerful language for event driven architectures such as WinForms and WPF. Alright, moving on. The second chapter of this week discusses files and streams. There's not much new here other than the existence of classes with static and instance methods used to make creating File and Memory Streams easier to work with. It's also possible to create fully qualified path names to files, get file and directory information, create files and directories on disk, etc...All in all, this chapter doesn't spend any time describing a feature of the language, as it does spending time exploring the System.IO namespace of the .NET Framework Library. The final chapter of the week explores the immutable nature of C# strings and ways in which this can be dealt with to create dynamic and complex strings. Well, that concludes the introduction to week 9. As usual, remember to ask questions here within the C# Workshop - Week 9 forum if you've got any. Additional Resources None...

Good Luck!

[Edited by - JWalsh on August 9, 2007 12:30:11 PM]
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Advertisement
I don't get events and delegates. The text doesn't really give me a good explanation. Can anyone provide an example? I'm just not seeing how to use it.

Beginner in Game Development?  Read here. And read here.

 

Quote:Original post by Alpha_ProgDes
I don't get events and delegates. The text doesn't really give me a good explanation. Can anyone provide an example? I'm just not seeing how to use it.


Delegates are unfortunately confusing since the term is used for multiple things.

The first term is for the keyword delegate:
public delegate void FooType(int Param);


This defines a delegate type. (note that it's often referred to as "delegate type" in common speech). If you've used C++, this is a typedef for void(int).

public FooType FooDelegate;


This creates an instance of FooType. This is also referred to as a delegate. It is the equivalent of a function object or function pointer in other languages. In this particular code though, FooDelegate is public, so anyone can assign to it, call it, append extra delegates to it (since delegates can contain more than one delegate, calling each in turn).

That kinda sucks, and makes debugging difficult so there's the event keyword.

 public event FooType FooEvent;


The event keyword is a modifier to public. It makes operator += and -= public, but everything else private. (yes private, subclasses cannot invoke the delegate)'


Hope that helps.

This topic is closed to new replies.

Advertisement