C# Workshop - Week 3 (Ch. 7 - 10)

Started by
2 comments, last by SamLowry 16 years, 6 months ago

Welcome to the GDNet C# Workshop – Ch. 7 - 10

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.

.NET Book Zero, Chapters 7 - 10

Introduction Hello everyone, and welcome back to week 3 of the C# Workshop. The first thing you'll notice right away is the existence of a new primary text. Rather than continue trudging along through the C# Specification we've decided to make a switch. From this point on, reading assignments and review questions will be taken from Charles Petzold's .NET Book Zero. The new books is smaller, less technical, and easier to read. It's my hope that with new reading material many people who felt previously overwhelmed will once again be able to dive into learning C#. It's unfortunate that our first attempt at finding a textbook left us feeling overwhelmed. C# is truly one of the most powerful and easy-to-learn languages. Hopefully this new text will prove effective. And speaking of the new text, this week we're covering four new chapters (7 - 10). Chapter 7 begins with a chart of all the operators in C#, along with their category and associativity. And while the associativity is less useful than the precedence it's still a very helpful table. For those new to programming, I encourage you to do as the reference says, and print that page. The rest of chapter 7 is spent exploring the different operator categories. For each one it explains what data types can be used with it, what the category is used for, and for many of them it explains what the return type is. These operators are the basis for all mathematical and logical operations which will be performed in C#. Learn this chapter well. After learning about the different operations we move on to chapter 8 where we cover selection and iteration. Selection is an unusual term, and is more frequently referred to as branching. You see, without some form of alteration, C# source code travels from the beginning of a method call to the bottom, without diverging. But there are generally two ways to effectively change that - making the execution pick a path to follow based on some kind of conditional comparisons, and making execution repeat a piece of code more than once (again, dependant upon a conditional check). These two things are the basis for this chapter. After the very practical chapters 7 and 8 we'll move on to a more theoretical chapter which provides a basic introduction to how memory is handled in C#, along with descriptions of the two memory containers - the stack and the heap (sometimes called the free store). Additionally, this chapter does a great job of identifying some of the key differences between value-type and reference-type variables, as they pertain to memory allocation. The last chapter for this week is on arrays. Quite frequently you'll need to work with sets of data rather than just a single piece of data. And while the .NET Framework Library provides access to classes which allow you to quickly and easily contain sets of data, the default and often easiest to use container is the array. This chapter explores array declaration, allocation, multi-dimensional, and jagged arrays. Well, that about covers the introduction. This resource is much lighter than the specification and the chapters are significantly smaller. I assume most people should have no problem covering the material but if you do, as usual, please do not hesitate to post your questions as threads here in this forum. Outline of the Reading - Chapters 7 - 10
  1. Operators and Expressions
  2. Primary Operators
  3. Unary Operators
  4. Multiplicative and Arithmetic Operators
  5. Shift Operators
  6. Relational Operators
  7. Equality Operators
  8. Logical Operators and Conditional Operators
  9. Conditional Operator
  10. Assignment Operator
  1. Selection and Iteration
  2. Selection Statements
  3. Iteration Statements
  4. Jump Statements
  1. The Stack and The Heap
  1. Arrays
Additional Resources None...

Good Luck!

[Edited by - JWalsh on July 21, 2007 11:17:03 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
Week 3 is now LIVE!

Start your reading! [lol]
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
Hi, i'm confused about the whole idea about the new operator. It keeps saying its going to explain it but I still do not understand what it does and why we need it. I know that its a constructor and that it is suppose to instantiate an object from a class but why do we have to instantiate the object?

Oh I'm not sure about how to apply the new operator. Thanks
Your terminology is a bit mixed up.

The new-operator is used to instantiate classes, which results in objects. It's like having a blueprint (= the class), and instantiating it into an actual building (= the object). The same blueprint can be reused many times to create many buildings (which will all look alike).

A class can define several constructors. When using new, you have to choose which constructor is used (how you choose: see later). Immediately after the object has been created, this constructor is run so that it can initialize the new object.

A simple example is a (mathematical) vector, let's go for 2D-vectors. These are pairs of numbers, indicating a position on a 2D-plane.

class Vector2D{    // First constructor    public Vector2D() { _x = _y = 0; }    // Second constructor    public Vector2D(float x, float y) { _x = x; _y = y; }    // Third constructor    public Vector2D(Vector2D other) { _x = other._x; _y = other._y; }    /* Some more stuff */    private float _x, _y;}

The first constructor just initializes the vector to (0, 0). The second can be used if you want to create a vector with the specified x- and y-coordinates. The third constructor creates a copy of another vector.

The new operator can be used to create Vector2D objects. It is mandatory that you select one of the constructors. This is done by giving the right arguments. The first constructor takes no arguments, so if you write
Vector2D vec = new Vector2D();

the first constructor will be called, and vec will represent the (0,0) vector. To call the second constructor, you need to provide two arguments of type float:
Vector2D vec = new Vector2D(1, 5);

This usage of new will use the second constructor.
(in case you're thinking "but 1 and 5 are ints, not floats, you're right, but the compiler will look for the best match and will convert 1 and 5 to 1.0f and 5.0f respectively).
An example of using the third constructor:
Vector2D vec1 = new Vector2D(1, 5);Vector2D vec2 = new Vector2D(vec1);

This topic is closed to new replies.

Advertisement