Advertisement

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

Started by July 01, 2007 12:15 AM
337 comments, last by paulecoyote 17 years, 3 months ago
Is having "using System" at the top of .cs files the norm? I know that in C++, "using namespace std" is shown in most example programs, but is generally frowned upon. Since the reason for not using 'using's in C++ (namely, that it defeats the purpose of namespaces) applies to C# as well, is this also just the norm in examples, but not encouraged in 'real' programs?
Quote: Original post by Ezbez
Is having "using System" at the top of .cs files the norm? I know that in C++, "using namespace std" is shown in most example programs, but is generally frowned upon. Since the reason for not using 'using's in C++ (namely, that it defeats the purpose of namespaces) applies to C# as well, is this also just the norm in examples, but not encouraged in 'real' programs?


Yes #using System is pretty normal for ever .cs file. Just like #using System.Windows.Forms is normal for any Windows UI .cs file. The only time #using statements get to be a problem is when you have namespaces that have the same Classes in them. the Windows and XNA namespaces do this.

theTroll

Advertisement
I personally don't think we'd gain much from extending the first week. This was supposed to be an introduction to all these concepts and expected to go far more in depth per topic as the weeks commenced. I think any remaining nuances will be cleared up in the coming chapters, especially with the advent of those very useful summaries from jwalsh and the other tutors.
I agree, the extra week wont help much, since both threads are slowing down in posts.
I'm not one of those who want to extend this part a week either, but i'm not totally against it. If it's going to be extended, maybe more exercises will make it easier to understand though. Well, I know we can always make experiment programs on our own, but it's much funnier to do exercises or solve programming problems.

Quote: Original post by JWalsh
  • What is the difference between an instance constructor and a static constructor?

  • Instance constructors are called when an object is created, to initialize the object. Static constructors are called by the CLR when a class is loaded, and is used to initialize the class itself.


    If instance constructors normally initializes their members when a new object is created, what good does a static constructor do? I cannot see what good a static constructor can do really, can someone give me an example please. To me it feels like it's against OOP somehow to use a static constructor, just because each object doesn't become unique. If the constructor is static, doesn't that mean that the constructor will be called only once, even if I make many instances of it?
    The main use of the static constructor is to initialize static members of your class.
    public class Test{    public static int Count;    public int someValue;    static Test()    {       Count = 5;    {     public Test(int in_value)     {        someValue = in_value;     )    }int current_count = Test.Count;  // This calls the static constructor first before it can be used the first time.Test myTest = new Test(10);  // This will first call the static constructor if it has not been called, then the instance constructor.


    Hope that explains it.
    theTroll

    Advertisement
    I don't know if this would be considered an advanced question, but didn't seem to belong in the other thread.

    Over the last few days I've coded some helper methods for doing math calculations - they all have return types like double, int, float...etc. That seemed like the best way to do it. But today I was looking at the XNA math methods (Vector2D...etc.) and saw that none of their methods use return types. Instead they all return void and provide an "out" parameter for the result.

    I'm assuming that if XNA does it that way then it must be the preferable way to write methods of this nature? If so, what are the negative impacts of doing it my way? What are the positive impacts of doing it their way?

    I would appreciate your thoughts...

    EDIT: On second thought, maybe this question ought to go in the For Beginners section. I posted here because I'm doing the workshop, but it doesn't have specifically to do with that. Sorry.
    Quote: Original post by JimDaniel
    I don't know if this would be considered an advanced question, but didn't seem to belong in the other thread.

    Over the last few days I've coded some helper methods for doing math calculations - they all have return types like double, int, float...etc. That seemed like the best way to do it. But today I was looking at the XNA math methods (Vector2D...etc.) and saw that none of their methods use return types. Instead they all return void and provide an "out" parameter for the result.

    I'm assuming that if XNA does it that way then it must be the preferable way to write methods of this nature? If so, what are the negative impacts of doing it my way? What are the positive impacts of doing it their way?

    I would appreciate your thoughts...

    EDIT: On second thought, maybe this question ought to go in the For Beginners section. I posted here because I'm doing the workshop, but it doesn't have specifically to do with that. Sorry.

    Imagine a 4x4 matrix of floats, each float is 4 bytes, 16 floats total. That's a good 64 bytes. Assuming your matrix is a value type, which it should be, passing around that raw matrix will result in a significant amount of copying. However, by passing ref/out parameters, you're simply passing a reference, which is significantly smaller. I've actually covered a lot of this in my journal.

    In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

    Quote: Original post by Washu
    Quote: Original post by JimDaniel
    I don't know if this would be considered an advanced question, but didn't seem to belong in the other thread.

    Over the last few days I've coded some helper methods for doing math calculations - they all have return types like double, int, float...etc. That seemed like the best way to do it. But today I was looking at the XNA math methods (Vector2D...etc.) and saw that none of their methods use return types. Instead they all return void and provide an "out" parameter for the result.

    I'm assuming that if XNA does it that way then it must be the preferable way to write methods of this nature? If so, what are the negative impacts of doing it my way? What are the positive impacts of doing it their way?

    I would appreciate your thoughts...

    EDIT: On second thought, maybe this question ought to go in the For Beginners section. I posted here because I'm doing the workshop, but it doesn't have specifically to do with that. Sorry.

    Imagine a 4x4 matrix of floats, each float is 4 bytes, 16 floats total. That's a good 64 bytes. Assuming your matrix is a value type, which it should be, passing around that raw matrix will result in a significant amount of copying. However, by passing ref/out parameters, you're simply passing a reference, which is significantly smaller. I've actually covered a lot of this in my journal.


    Thanks. That gives me some perspective on why its done that way. I guess I ought to check out your journal then... :-)
    Quote: Original post by JimDaniel
    Quote: Original post by Washu
    Quote: Original post by JimDaniel
    I don't know if this would be considered an advanced question, but didn't seem to belong in the other thread.

    Over the last few days I've coded some helper methods for doing math calculations - they all have return types like double, int, float...etc. That seemed like the best way to do it. But today I was looking at the XNA math methods (Vector2D...etc.) and saw that none of their methods use return types. Instead they all return void and provide an "out" parameter for the result.

    I'm assuming that if XNA does it that way then it must be the preferable way to write methods of this nature? If so, what are the negative impacts of doing it my way? What are the positive impacts of doing it their way?

    I would appreciate your thoughts...

    EDIT: On second thought, maybe this question ought to go in the For Beginners section. I posted here because I'm doing the workshop, but it doesn't have specifically to do with that. Sorry.

    Imagine a 4x4 matrix of floats, each float is 4 bytes, 16 floats total. That's a good 64 bytes. Assuming your matrix is a value type, which it should be, passing around that raw matrix will result in a significant amount of copying. However, by passing ref/out parameters, you're simply passing a reference, which is significantly smaller. I've actually covered a lot of this in my journal.


    Thanks. That gives me some perspective on why its done that way. I guess I ought to check out your journal then... :-)

    Be forewarned, my journal isn't light reading for many people (I find that it is, but then again I wrote it). I will be glad to answer any questions you have about the contents though.

    In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

    This topic is closed to new replies.

    Advertisement