🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C# Workshop - Week 8 (Ch. 20 - 23)

Started by
-1 comments, last by JWalsh 16 years, 11 months ago

Welcome to the GDNet C# Workshop – Ch. 20 - 23

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 20 - 23

Introduction Welcome to week 8 of the C# Workshop. After a week break for review and work on Project 2, we're back in action. This week we'll be covering 4 chapters, however only the first three are of real use to the programmer. The last chapter of this week is on the DateTime object which is provided by the .NET Framework Library. After the author spent 3 or 4 chapters writing his own Date class, which he used to illustrate various components of the C# language, he felt it necessary to review how the .NET Framework implemented the "real" DateTime object. And while it's useful to know, it doesn’t really teach anything more of the language, and it's nothing which couldn't have been gleaned on our own from reading the Class Documentation on MSDN. With that aside, lets briefly talk about the 3 chapters we cover that DO teach something new about the language. The first chapter of the week is about operator overloading. This chapter spends a brief amount of time discussing all of the different types of operator overloading, including unary, binary, conditional, equality/inequality and even implicit and explicit casting operators. Although the text doesn't go into a lot of detail, it does cover the basics. The core idea is that operator overloading allows you to control how operators interact with your objects in C#. If you create a class or struct which might logically have use for addition, subtraction, comparison, or equality, then you can overload those operators to make it easier to work with your classes. However, as pointed out in the text, this is syntactic sugar, and although it works in C#, it doesn't necessarily work in .NET languages. As a result, it's important to have methods appropriately named which also perform the desired mathematical or logical operations. And also as pointed out in the text, there's no reason to duplicate code, so you might as well have your operator overload refer to the named method directly. After spending time with operator overloading we move on to Interfaces. This is a bit of a jump back, and should have perhaps come before the chapter on Operator overloading, as Interfaces are a more core Object Oriented Design philosophy and should have been lumped with the chapter on Inheritance and Virtuality. At any rate, just as you can derive a class from a base class, you can specify that a class implements an interface. This syntax is the same for the derived class, and the primary difference is the use of the keyword 'interface' rather than 'class' in the base class. The other important difference is that while base classes can provide their own implementations, interfaces cannot. They can contain methods, properties, and indexers, however they cannot have fields or implementations for any of their methods, etc....Interfaces exist solely for the purpose of creating a compiler-enforced way of guaranteeing that classes which implement the interface have created their own implementations of each of the methods provided by the interface. The final chapter of merit this week is on Interoperability, and while this chapter is brief it does illustrate a fundamental capability of C#. And that is the ability to invoke code on any Native C/C++ dll. This is primarily useful when developing Windows GUI applications that require calls to the native Win32 API or Platform SDK. The important concepts with p/Invoke are first that you must define an external method within your class, which has an attribute that specifies which DLL the actual method implementation is located in. And secondly that any structures passed in to or returned out of the external method must be locally defined. In short, this means that C# has no knowledge of the structures required by the method, and so you must create your own which has the same memory layout as that required by the external function you'd like to call. Well, that concludes the overview of Week 8. As usual, feel free to ask questions here in the C# Workshop - Week 8 Forum. Additional Resources None...

Good Luck!

[Edited by - JWalsh on August 9, 2007 11:54:40 AM]
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

This topic is closed to new replies.

Advertisement