Wednesday, July 18, 2007

Chapter 1.2

Chapter 1.2 - Program Structure - to be continued later



Following suit with the Lanuage Specification

1.2 Program Structure

Hierarchieal structure of C#
1. Programs
2. Namespaces
3. Types
4. Members
5. Assemblies


1. Programs
Made up of one or more source files.

2. (revisiting)Namespaces - Namespaces simply hold information. Two namespaces can have the same information. A good example of this is one i have reworked thats is found in chapter three...


namespace helloworld1
{
class hithere
{

static void Main() {
Console.WriteLine("Hello, World");
}

}

}

namespace helloworld2
{
class hithere
{
static void Main() {
Console.WriteLine("Hello, World");
}

}

}

As you can see the two namespaces above contain the same class and the same helloworld method as above. Don't think too much into it. I will do that for you.

Game programming example. Why you would use two different namespaces, with the same information.
Lets take any game with Swords in it. Swords are very similar to each other. They would have a size, maybe a sharpness, or even a weight.
These properties all apply to a sword so you might have a namespace for say the "Game - Zelda", and one for "game - Final Fantasy", each
containing a sword class, each containing the properties os size, sharpness, and weight.


Now lets take a look again at that line, using System; ,
This line references the System Namespace, which contains a bunch of stuff that is used in console applications, like this HelloWorld application uses,
if your wondering whats in it, i have no clue, but i am sure google does.

For this application we need to use the System namspace because it contains the Console class, translation: its got what we need to make the console write a line of text, etc.

3. Types - There are two type of Types i will go through just about every type and give a small explaination/range of each, hopfully to spark proper use of types,( and not always just using int.)

I. Value Types - i. Simple Types, ii. Enum Types, and iii. Struct Types.
II. Reference Types - i. Class Types, ii. Interface Types, iii. Array Types, and iv. Delegate Types.

I. Value Types
i. Simple Types - predefined struct types
1. Signed Intergral
2. Unsigned Integral
3. Unicode Characters
4. IEEE Floating Points
5. High Precision Decimal
6. Boolean

1. Signed Intergral - Signed Integral numbers are either Positive or Negative values
a. sbyte -128 to +127
b. short -32,768 to +32,767
c. int -2,147,483,648 to +2,147,483,647
d. long -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

2. Unsigned Integral - Unsigned Integeral numbers are only Positive Values
a. byte 0 to +255
b. ushort 0 to +65,535
c. uint 0 to +4,294,967,295
d. ulong 0 to +18,446,744,073,709,551,615

3. Unicode Characters- Simply any single character on the kyboard.

4. IEEE Floating Points - Non-Precise Decimal Numbers (are rounded by your machine)
a. Float - ±1.5e-45 to ±3.4e38 Precision : 7 places
b. Double - ±5.0e-324 to ±1.7e308 Precision : 15 - 16 places

5. High Precision Decimal - Precise Deciaml Numbers
a. Decimal - ±1.0 × 10-28 to ±7.9 × 1028 Precision: 28 - 29 places
6. Boolean - True or False
a. bool - Store the Value of True (1) or False (0)

ii. Enum Types - Define a set of related Constants

1. User-defined types of the form enum E {...}

No two Enumerated Types can have the same name. I am sure an example is nessesary here.

Example:
enum Swords
{
MasterSword,
Excalibur = 10,
GreatSword
}

iii. Struct Types - Light weight objects , and can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types

You can think of struct as scaled down classes, and are used to group data whwhich is relative. Structs are defined by :

1. [attributes] [modifiers] struct identifier [:interfaces] body [;]

Structs can be instantiated with or without the new operator
The difference between classes and structs are in their type. Structs are a value type (stored on the stack) while classes are a reference(stored on the heap).
Example using a class

public void swordswing1()
{
Sword MasterSwordPower = new Sword();
Sword ExcaliburPower = MasterSwordPower;
}
Example using a struct

public void swordswing1()
{
Sword MasterSwordPower = new Sword();
Sword ExcaliburPower = MasterSwordPower;
}
Because structs are store on the stack, you are assigning object to object, losing efficientcy, while the class stores a reference to the object on the stack.
Stucts do not support inheritance.
Struct do support contructors, which are method with the same name as the struct

using System;

struct Sword
{
private string Name;
private string Type;

//Structs' Constructor
public Sword(string name, string type)
{
Name = name;
Address = address;
}

(Continued in next post)

II. Reference Types ( going to be finished in next post)
i. Classes (reused from first lesson)- I have always been told that a class is like a blue print, so i am going to go on that pun. (reused from first lesson)

A class is like a blue print, lets use swords again. Like mentioned above you have your class sword, that contains properties such as size, weight, and sharpness.
Never mind syntax for right now, or the attributes within the members.



class Sword
{
Size
{
small
medium
big
}
Weight
{
light
heavy
}

Sharpness

{
dull
sharp
}

}

When you think about it, the class sword, will at some point become an object when implemented, without the members, which are in this case, size, weight, and sharpness,
we would have the same type of sword being created, the attributes within the members define a unique object, or plain object.

why? lets assume sword is the base of all other swords

Lets use another example

You are trying to create the Master sword, The want the Master sword to inherit all of the attributes of the Sword class, but have new attributes, like an
attack power.

class Master Sword : Sword
{
Attack Power
{
10000000
}

}


Master Sword Inherits all of the attributs of sword , which are weight, sharpness, and size, and has another member called Attack power.

This is a rouge outline of inheritance, remember we are only focusing on understanding basic concepts, syntax is not a concern right now for examples.


No comments: