C# Workshop - Week 2 (Ch. 5 - 6)

Started by
22 comments, last by SpiderPig 16 years, 7 months ago

Welcome to the GDNet C# Workshop – .NET Book Zero Ch. 5 - 6

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. Additional Resources None...

Good Luck!

[Edited by - JWalsh on July 23, 2007 9:44:06 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
As I will be out of town from Tomorrow afternoon until Tuesday, I wanted to go ahead and post the chapter thread for week 2 now. I will be filling in the overview and exercises for week 2 from my laptop as I get them finished - and get access to the internet.
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
great Job man, i have been having a lot of fun getting into c#. I hope your weekend goes fine and i will be waiting for this weeks assignments ;)
Awesome Jeromy, thanks a lot! I'll definitely read through the material, but the review questions are scaring me... 100? That's double week one! >_>
Variables are a large topic, and one that should be completely understood before moving on to anything else. Without a good foundation with variables, you will be way over your head with the rest of the stuff.

However, if you have done programming in another language, this chapter should probably be pretty easy for you. There are several nuances to C# variables, but for the most part they behave like other languages.

I would also like to applaud JWalsh for the amazing amount of work he is doing writing out these overviews, organizing the whole thing, and writing review questions. I know I wouldn't have the time to do it!
Mike Popoloski | Journal | SlimDX
I had to be away from my computer for five days so I thought I would've had to play catchup for a while. Didn't have any idea you would delay week 2, guess I caught a break there. :D

Quote:
Note that base classes do not contribute to the declaration space of a class, and base interfaces do not contribute to the declaration space of an interface.


So what happens to the inherited declarations anyways? Does the declaration space for the inherited class get a copy of all its base class declarations, or are you essentially calling base.Member everytime you use a base class's members?
The derived class does receive a "copy" of sorts of all methods and variables from the base class. The only time you need to use the base keyword is when you are overriding a base class's property or method, and you want to call the base class's implementation.
Mike Popoloski | Journal | SlimDX
Quote:Original post by Menace2Society
I had to be away from my computer for five days so I thought I would've had to play catchup for a while. Didn't have any idea you would delay week 2, guess I caught a break there. :D

Quote:
Note that base classes do not contribute to the declaration space of a class, and base interfaces do not contribute to the declaration space of an interface.


So what happens to the inherited declarations anyways? Does the declaration space for the inherited class get a copy of all its base class declarations, or are you essentially calling base.Member every time you use a base class's members?


The inherited class uses the base class public and protected methods and members as long as it has not overridden or hid the methods. This is done without the programmer having to explicitly call the base class method. It does not really get a "copy" of the methods. It just calls the base class methods.

theTroll
Quote:So what happens to the inherited declarations anyways? Does the declaration space for the inherited class get a copy of all its base class declarations, or are you essentially calling base.Member everytime you use a base class's members?


You are indirectly calling base.Member every time you use a base class's member - well, a base class's methods, anyways. Fields are a bit different.

You see, programming languages such as C++ and C# use something called a Dispatch Table or Virtual Table (vtable for short). This vtable is a hidden member of all classes which holds the addresses to each of the dynamically bound methods of the class. In this way, every object of a related type shares a similar vtable and the memory required to store a method's implementation isn't duplicated.

Whenever you inherit from a base class the public, protected, and private fields are added to the classes scope and internal layout, while methods, indexers, etc...are added to the derived class's vtable.

It's important, however, to keep in mind the distinction between a member's declaration space and a member's scope. You see, scope simply defines where a member is accessible without requiring qualification.

When you inherit a base class, the derived class's scope is increased to include the members of it's base class. In this way it can refer to those members directly. Meanwhile, the members of the base and derived class remain in separate deceleration spaces. This means it is not possible to add members to a base class from within a derived class. It also means that you can create members in the derived class with the same name as members in the base class, thus allowing for name hiding. (See section 3.7.1 for more info on Name Hiding)

So to summarize, scope is increased in the derived class to include the base class, this allows for referring to the base classes members by name. The fields of the base class are added to the derived class, while methods, indexers, properties, etc...are added to the derived class's virtual table. Even though scope has increased, the deceleration space remains distinct, thus allowing for name hiding.

Cheers!
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
Quote:Original post by TheTroll

The inherited class uses the base class public and protected methods and members as long as it has not overridden or hid the methods. This is done without the programmer having to explicitly call the base class method. It does not really get a "copy" of the methods. It just calls the base class methods.

theTroll


Okay so if this is the case, then what does it really mean to say that a constructor isn't inherited but everything else is? Since to access any inherited class members you'd have to call on the base class, does that mean the only real difference is you have to explicitly call a base class constructor while every other element is accessed implicitly?

This topic is closed to new replies.

Advertisement