🎉 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 1 (Ch. 1 & 2) - Advanced

Started by
337 comments, last by paulecoyote 17 years ago
Quote: Original post by hpjchobbes
When would a programmer need to use the LOCK statement in a program?

Short answer: synchronisation.

Longer answer: When (s)he's using multithreading and needs to make sure the shared state is handled correctly.

Answer in the context of "workshop's first week": I don't know your experience with these topics, so I'm just going to say "Don't worry about it now, you're not going to have to use it yet. When you do, you'll know."
Advertisement
Quote: Original post by hpjchobbes

When dealing with a class that inherits from a base class, the document states: " Inheritance means that a class implicitly contains all members of its base class, except for the constructors of the base class". In the example, they show a Point3D class that inherits from a Point class. The Point3D's constructor inherits from the Point constructor. Does the inherited constructor ever get run? In the example, do X and Y not get values since the Point3D constructor didn't specify?

Think of a class hierarchy (A, B inherits from A, C inherits from B) as a tower. A would be the lowest level, C the highest. When building such a tower, the levels must be built in order: first level A, then B, then C. Same thing happens with constructors. First, one of A's constructors must be called, then one of B's constructors, and lastly, one of C's.
So to answer your question: Point's constructor does get to run indeed, and it runs first.

public class Point3D: Point{    public int z;    public Point3D(int x, int y, int z)        : Point(x, y)                    // <-----    {        this.z = z;    }}

The arrow points to the code which shows which of the base's constructors (there can be more than one due to overloading) needs to be called with what arguments. If that line were omitted, it would be interpreted as an implicit call to the default constructor (which structs always have, classes not necessarily).

Paragraph 10.10.1 in the spec explains fully how constructors are called.
As an example:

class A{    A()          { ... }    A(int x)     { ... }    A(string s)  { ... }}class B : A{    B()                           { ... } // construction order: A(), B()    B(int x) : base(x.ToString()) { ... } // order: A(string), B(int)}class C : B{    C() : base(5)     { ... }  // order : A(string), B(int), C()    C(string s)       { ... }  // order : A(), B(), C(string)}





What it means for constructors not to be inherited (using the spec's Point example):
Point provides two constructors:
  • one implicit default constructor taking 0 arguments and initializing x and y to 0

  • one taking 2 integer arguments


Point3D provides two constructors: one with 0 arguments (x, y, z = 0) and one with 3 int-arguments.

If constructors were to be inhereted, Point3D would have a third constructor: the one from Point taking 2 arguments. This is not the case.

[Edited by - SamLowry on July 1, 2007 4:02:33 AM]
Quote: Original post by hpjchobbes
Last, I'm not sure I understand what "Metadata" is, other than the document states that it contains symbolic information. What does this mean?

Don't worry too much about it, it'll become clearer when we'll discuss attributes.


Metadata is "data about data". Consider MP3s: the data they contain is audio. The ID3 tags, containing information such as genre, lyrics, composer, album, rating, etc. gives extra information about the audio, hence ID3 can be called metadata.

Another example is the extra data the OS keeps about your files: creation date, last modification date, security settings, etc. all these are metadata.


From Wikipedia:
Quote:
.NET metadata, in the Microsoft .NET framework, refers to code that describes .NET CIL (Common Intermediate Language) code. A .NET language compiler will generate the metadata and store this in the assembly containing the CIL. Metadata describes all classes and class members that are defined in the assembly, and the classes and class members that the current assembly will call from another assembly. The metadata for a method contains the complete description of the method, including the class (and the assembly that contains the class), the return type and all of the method parameters. When the CLR executes CIL it will check to make sure that the metadata of the called method is the same as the metadata that is stored in the calling method. This ensures that a method can only be called with exactly the right number of parameters and exactly the right parameter types.


You can also add your own metadata using attributes (will be discussed later). There's much to be said about this topic, but again, as this is the first week... let's leave something for the 9 remaining weeks.
Quote: Original post by hpjchobbes
On a similar topic to class inheritance, what's the difference between inheriting a function vs. overriding the function? It would seem that if you inherit then the base function would still get called, but I'm not sure.

I'm not sure about how to answer this one.

Quote:
When a type inherits from a base class, all members of the base class, except instance constructors, destructors and static constructors, become members of the derived type.

Inheriting means that all members (except for ...) of the base class are included in the subclass. Overriding means giving a new meaning to these members.

// Only relevant members are shownclass Rectangle{    public float Width { get { ... } }    public float Height { get { ... } }    public virtual float Circumference    {        get { return Width * 2 + Height * 2; }    }    public virtual float Area    {        get { return Width * Height; }    }}// A square is a rectangle with Width == Heightclass Square : Rectangle{   public float Side { get { return Width; } }   public override float Circumference   {        get { return Side * 4; }   }}

In this example, Square inherits both Circumference and Area but chooses to only override Circumference. This choice is completly arbitrary: I could have overridden both, or neither, the code will still generate the same results. Providing specialized versions in this case has a (small) impact on efficiency: more specialized algorithms can make more assumptions and hence use shortcuts to compute the result faster.

So, when computing the circumference of a Square, its own specialized version will be used. A Square's area will still be computed using Rectangle's logic.
2: What are the keys to software components in C#?
- Programs, namespaces, types, members and assemblies.
- - "C# programs consist of one or more source files. Programs declare types, which contain members and can be organized into namespaces. When C# programs are compiled, they are physically packaged into assemblies."

11: What's the name of the C# Runtime libraries?
- .NET

48: What is method overloading?
- This is when one or more methods have the same name but are distinguished by the arguments and return type.

58: What is an array?
- It is a data structure that contains many variables accessed using an index.
I have a question when answering question 1. Is it correct to say that object-oriented programming is a type of component-oriented programming? It seems to me that OOP is a type of COP because OOP helps you create "self-contained and self-describing packages of functionality".
Quote: An implicit conversion exists from a class type to any of its base class types. Therefore, a variable of a class type can reference an instance of that class or an instance of any derived class. For example, given the previous class declarations, a variable of type Point can reference either a Point or a Point3D:
Point a = new Point(10, 20);
Point b = new Point3D(10, 20, 30);


Does this mean I can access the derived class' member functions if I cast it as a "Point"? i.e "b.setZ = 1337;"
Quote: Original post by Ezbez
I have a question when answering question 1. Is it correct to say that object-oriented programming is a type of component-oriented programming? It seems to me that OOP is a type of COP because OOP helps you create "self-contained and self-describing packages of functionality".


I don't understand the difference between the two. OOP does all the things that COP is described to do in the document. You can have components in OOP.

Quote: C# is an object-oriented language, but C# further includes support for component-oriented programming. Contemporary software design increasingly relies on software components in the form of self-contained and self-describing packages of functionality. Key to such components is that they present a programming model with properties, methods, and events; they have attributes that provide declarative information about the component; and they incorporate their own documentation. C# provides language constructs to directly support these concepts, making C# a very natural language in which to create and use software components.


It seems to me it's just a lot of fancy talk for "we facilitated OOP a little more" but decided to give it a different name. Someone tell me why I'm wrong?
Quote: Original post by finky45

It seems to me it's just a lot of fancy talk for "we facilitated OOP a little more" but decided to give it a different name. Someone tell me why I'm wrong?


COP is more then just OOP with components. COP is a change in mindset much like OOP was a change from functional programming. In COP the interface is the center, like in OOP the object was the core. COP makes it so the design should support versoning and seperating the interface from the implimination. It brings the "black box" approach a little closer to reality.

theTroll

Quote: Original post by finky45
Quote: An implicit conversion exists from a class type to any of its base class types. Therefore, a variable of a class type can reference an instance of that class or an instance of any derived class. For example, given the previous class declarations, a variable of type Point can reference either a Point or a Point3D:
Point a = new Point(10, 20);
Point b = new Point3D(10, 20, 30);


Does this mean I can access the derived class' member functions if I cast it as a "Point"? i.e "b.setZ = 1337;"

No. The compile-time type determines which members you can access. In your case this type is Point, meaning access to Point3D-members is not allowed.

This topic is closed to new replies.

Advertisement