Advertisement

C# Workshop - Week 1 (Ch. 1 & 2) - Advanced

Started by July 01, 2007 12:15 AM
337 comments, last by paulecoyote 17 years, 2 months ago
Thanks for the clarification, Telastyn. For further reference concerning my other question, it is also possible for Main() to return an int in C#, according to MSDN. I have a few more questions regarding Chapter I before moving on though. I have a background in C++, so some of my questions might relate to C++ concepts.

  1. Considering the following code snippet from the specification:
    public class Point3D: Point{    public int z;    public Point3D(int x, int y, int z): Point(x, y)     {        this.z = z;    }}
    How come the compiler complains when calling the base constructor as so? It expects base() or this() instead. Am I missing something?

  2. Out of curiosity, are the Windows Forms properties actual C# properties?

  3. Quote: C# Specification, p.25
    ...it is not possible to create references to structs, which rules out their usage in a number of situations.

    What are some examples of such situations?

  4. Are default parameters permitted in C#?

  5. Is it possible to separate a method's implementation from its declaration (i.e. not cluttering the class' declaration with function bodies)?

  6. How can one declare a method to be const (i.e. unable to modify this)?

  7. If I understood correctly, it's impossible to provide an implementation to an abstract method for overridden ones to chain up, right? Also, are explicit calls to base methods that have been overridden allowed?

  8. Considering the following code snippet:

    int i = new int();

    Is i placed on the stack or allocated dynamically? I'm guessing it's the former. If so, is it possible to declare a reference to an int other than using ref?

By the way, I appreciate the tutors' commitment in this workshop. I've been reading this thread from the beginning, and it's amazing how much insight I have gained just from reading answers for other students' questions. Keep up the good work guys!

[Edited by - Darklighter on July 5, 2007 11:32:15 AM]
Quote: Original post by Darklighter
1. Considering the following code snippet from the specification:
public class Point3D: Point{    public int z;    public Point3D(int x, int y, int z): Point(x, y)     {        this.z = z;    }}
How come the compiler complains when calling the base constructor as so? It expects base() or this() instead. Am I missing something?

I guess it's an error in the spec.

Quote: Are default parameters permitted in C#?

No. You'll have to rely on overloading.

Quote:
Is it possible to separate a method's implementation from its declaration (i.e. not cluttering the class' declaration with function bodies)?

No. If you need an overview of the class members, the IDE can provide it for you.

Quote:
How can one declare a method to be const (i.e. unable to modify this)?

You can't in C#.

Quote: If I understood correctly, it's impossible to provide an implementation to an abstract method for overridden ones to chain up, right?

Well, not sure I understood your question, but you could make the method just virtual, which is an abstract method with a body. Does this answer your question?

Quote: Also, are explicit calls to base methods that have been overridden allowed?

Yes. Use base.<method-name>(); See 7.5.8.

Quote: Is i placed on the stack or allocated dynamically?

On the stack.

Quote: If so, is it possible to declare a reference to an int other than using ref?

Depending on what you want a ref for, you could

  • rely on (auto)boxing (spec 4.3);

  • write your own class which wraps around an int;

  • use int? which is the same as Nullable<int> (spec 19.5);

  • maybe some other option I haven't thought of.



[Edited by - SamLowry on July 5, 2007 12:55:04 PM]
Advertisement
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.


This has probably already been answered, but hey I can always do it again anyways. I got in a bit late to this workshop, is all.

When overriding a function, you are essentially replacing the original function with a new one.

[fact]Or at least this is true in the case of abstract function defintions in abstract classes[/fact]. I'm not entirely sure about virtual functions, but I am pretty sure that if you make a virtual function and override it, the original version will not be called, only the overridden one.
_______________________Afr0Games
Quote: Original post by Afr0m@n
[fact]Or at least this is true in the case of abstract function defintions in abstract classes[/fact]. I'm not entirely sure about virtual functions, but I am pretty sure that if you make a virtual function and override it, the original version will not be called, only the overridden one.


virtual == abstract + some body, meaning overriding virtual methods and overriding abstract methods is the same thing.

From 1.6.5.4:
Quote:
An abstract method is a virtual method with no implementation. An abstract method is declared with the abstract modifier and is permitted only in a class that is also declared abstract. An abstract method must be overridden in every non-abstract derived class.
Quote: Original post by SamLowry
No. If you need an overview of the class members, the IDE can provide it for you.


Out of curiosity, when I use Go to Definition on Console.WriteLine(), how come it provides me with a class without implementations? Is this code the actual meta-data from the assembly?

Quote: Original post by SamLowry
Well, if I understand correctly, you could make the method just virtual, which is an abstract method with a body.


Alright, but in this case the class wouldn't necessarily be abstract. I'm asking because in C++ you can implement an abstract method with common functionality that derived classes can use when overriding it.
i
Quote: Original post by Darklighter
Thanks for the clarification, Telastyn. For further reference concerning my other question, it is also possible for Main() to return an int in C#, according to MSDN. I have a few more questions regarding Chapter I before moving on though. I have a background in C++, so some of my questions might relate to C++ concepts.


I will see how many of these I can help you with.

Quote:
  1. Considering the following code snippet from the specification:
    public class Point3D: Point{    public int z;    public Point3D(int x, int y, int z): Point(x, y)     {        this.z = z;    }}
    How come the compiler complains when calling the base constructor as so? It expects base() or this() instead. Am I missing something?


The correct C# way of doing this;
[source lang=c#]public class Point3D: Point{    public int z;    public Point3D(int x, int y, int z):base(x, y)    {        this.z = z;    }

Quote:

  • Out of curiosity, are the Windows Forms properties actual C# properties?



  • Yes they are actually properties.

    Quote: C# Specification, p.25
    ...it is not possible to create references to structs, which rules out their usage in a number of situations.

    What are some examples of such situations?

    The biggest ones that I can think of is when you have a struct that is large or when you have to pass it around a lot and it is medium size. [opinion]Any time I have a struct that gets pretty large I convert it over to a class, so it will be used by reference instead of by value.[/opinion]

    Quote:
  • Are default parameters permitted in C#?



  • No it does not, well not directly. That is what overloading is for.
    [source lang=c#]public int foo(int a, float b, string c){    /// Do something  with a, b, c}public int foo(int a, float b){    string c = "Some string";    /// Do something  with a, b, c}public int foo(int a){    string c = "Some string";    float b = 1.8f;    /// Do something with a, b, c}public int foo(){    string c = "Some string";    float b = 1.8f;    int a = 7;    /// Do something with a, b, c}


    That gives you the same abilities of default parameters.

    Quote:
  • Is it possible to separate a method's implementation from its declaration (i.e. not cluttering the class' declaration with function bodies)?


  • Not really, there are partial classes, but that doesn't really do what you are wanting to do.

    Quote:
  • How can one declare a method to be const (i.e. unable to modify this)?



  • Not sure what you are looking for, can you explain?

    Quote:
  • If I understood correctly, it's impossible to provide an implementation to an abstract method for overridden ones to chain up, right? Also, are explicit calls to base methods that have been overridden allowed?



  • Right, abstract methods have no implementation in the base class. As for the second part, it depends. Going to suggest you read this; http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_10_5_4.asp

    Quote:
  • Considering the following code snippet:

    int i = new int();

    Is i placed on the stack or allocated dynamically? I'm guessing it's the former. If so, is it possible to declare a reference to an int other than using ref?

  • From what I understand, the stact. And no, ref and out are the only way to referece a value type.

    Hope that helps.
    theTroll
    Advertisement
    Quote: Originally by Darklighter:
    Out of curiosity, when I use Go to Definition on Console.WriteLine, how come it provides me with a class without implementations? Also, what does [From metadata] refer to?
    You get a class without implementation because you don't actually have the source code -- the original implementation -- for System.Console anywhere. What you do have, however, is the .NET assembly that contains the IL and metadata for the class. The IDE can use reflection to read this metadata and produce a skeleton representing the classes interface -- that's what you're seeing, and what's what the "from metadata" tag means.

    See this page, scroll down to "Go To Definition Shows Reflected Source (C# only)" or look for Figure 45.
    15. When are forward declarations needed in C#?
    19. What is the encoding of a C# character, and how much memory does a
    single character require?
    25. What is the relationship between operators and operands in an
    expression?
    49. What is the difference between an instance constructor and a static
    constructor?


    A little help on those questions please.
    Quote: Original post by Darklighter
    Quote: Original post by SamLowry
    No. If you need an overview of the class members, the IDE can provide it for you.


    Out of curiosity, when I use Go to Definition on Console.WriteLine(), how come it provides me with a class without implementations? Is this code the actual meta-data from the assembly?

    I guess the IDE generated this code based on the type information stored in the assembly. It certainly isn't valid C# code.

    Quote:
    Quote: Original post by SamLowry
    Well, if I understand correctly, you could make the method just virtual, which is an abstract method with a body.


    Alright, but in this case the class wouldn't necessarily be abstract. I'm asking because in C++ you can implement an abstract method with common functionality that derived classes can use when overriding it.

    Yes. You can do that too in C#. The difference between C++ and C# is that C++ let's you have several base classes (multiple inheritance), while C# relies on interfaces, but these do not allow you to specify default behavior.
    Quote: Original post by shawnre
    15. When are forward declarations needed in C#?


    Never.

    Quote:
    19. What is the encoding of a C# character, and how much memory does a
    single character require?

    A 16 bit unicode character.

    Quote:
    25. What is the relationship between operators and operands in an
    expression?


    Operators are specifically designed to produce a new value for the values being operated on. The values being operated on are operands.

    Quote:
    49. What is the difference between an instance constructor and a static
    constructor?


    A static constructor is used to access static properties and methods. It is called before you access them. An instance constructor is used when ever you create an object in memory.

    TheTroll

    This topic is closed to new replies.

    Advertisement