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
Quote: Original post by Strewth
I've been using C# for probably 3 or 4 years now and have never felt the need to use a Struct. What circumstances warrant it?

Structures are not strictly heap allocated. By default, creating an instance of a structure within a method will result in it being created on the stack, and released from the stack when the method ends. Furthermore, structures are value types. You use them all the time, integers and floats being two prime examples of structures you use frequently. As they are value types, they are passed by value, which means that large structures, when passed, will result in a large amount of copying.

Structures are ideal to use in instances where having small heap allocations standing around will increase GC counts and decrease performance. For instance, matrix multiplication used in games for transformations typically results in large numbers of temporary matrix instances laying around. If those were heap allocated, then it could push other slightly longer lived temporaries up into gen1, which would end up resulting in slower collections by the GC (since it will have to clean up gen1 stuff as well). There are other reasons as well, sometimes it just makes sense. A complex type would be best represented as a struct, since that most readily expresses its usage semantics.

Note: You can pass structures via ref and out, thus reducing copying (it also will increase the chances of your code being inlined, as I've covered 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.

15. When are forward declarations needed in C#?

From the specs: Forward declarations are never needed in C# because, with very few exceptions, declaration order is insignificant.

When might there be an exception?
-Brandon Blog | Journal
Advertisement
Most of the questions I have answered fine but these ones I am unsure about

What's the difference between a virtual and non-virtual method?
- virtual methods can be overridden by derived classes.


What does it mean to override a virtual method?
- It means provide a new and more specialised version of the method for a derived class.


What is an abstract method and when is it permitted?
- An abstract method is a method which is only there to enforce an action which derived classes must provide and implement if it is to be derived from that base class.

What is an event, and what is true of it's type?
An event is the part of a class which signifies when internal changes are made to the class


What event handlers?


What is a class operator?
A class operator is when a class overloads an operator to be used on instances of the class.

What is a delegate?
Delegates are like function pointers???


What is the purpose of attributes?
Attributes are there to provide hints on how the code is used??

What is contained within the first line of a grammar rule?
A non-terminal symbol???

Also are punctuators things like brackets(), square brackets[] and curly braces{}?.
How about them apples?
popcorn, most of your answers are fine, here's some differences from what I had when writing the review questions:

What's the difference between a virtual and non-virtual method?
When a virtual method is invoked, it's runtime type for the instance is invoked. For a nonvirtual method, the compile-time type is invoked.

Note: Your answer of virtual methods being able to be overriden is another difference, just not the one I was reading about at the time.

What is an abstract method and when is it permitted?
A method with no implementation. It is only permitted in classes also declared abstract.

What is an event, and what is true of it's type?
An event is a member that enables a class or object to provide notifications. The type must always be a delegate.

What are event handlers?
Event handlers are methods attached to an event using the += operator and removed with the -= operator.

Note: Looks like there was a typo when posting this, let me go back and fix it.

What is a delegate?
A delegate represents references to methods with a particular parameter list and return type. Delegates make it possible to treat methods as variables which can be assigned and passed as parameters.

What is the purpose of attributes?
To allow user-defined types of declerative information that can be attached to program entities and retreived at run-time.

Hope these help everyone. Cheers!
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
Quote: Original post by brandogon
15. When are forward declarations needed in C#?

From the specs: Forward declarations are never needed in C# because, with very few exceptions, declaration order is insignificant.

When might there be an exception?


I think the author was being unintentionally ambiguous. I did a search and found an alternate version of the C# Specification on MSDN. In the getting started section of the guide it compares the C# Hello World program to what might be included in a similar C++ version. It identifies some of the things missing from the C# version and says the following:

Quote: The program does not contain forward declarations. Forward declarations are never needed, as declaration order is not significant.


Here's the Source for people wishing to check out a second reference.

Cheers!
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
Should we continue sticking with the specification you linked at the beginning or look into the one provided on msdn?
Advertisement
Quote: Original post by dusty_one
Should we continue sticking with the specification you linked at the beginning or look into the one provided on msdn?


No, continue using the downloadable specification I provided in the beginning.

The C# Spec on MSDN just appears to have some small changes and formatting to it. I suspect that Microsoft continues to update the online version, while leaving the .doc printable format relatively stagnant. This means the online version is basically just the print version with some additional errata/clarifications worked in.

If there's a particular subject which is giving you grief, you may try and look it up on the MSDN website to see if there is any further information. Other than that, I want to make sure we're all using the same reference material, even if it may be a bit rough around the edges.

Cheers!
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
Some of these topics have already been covered and answered, but I felt they were very distributed, so I'm going to try to summarize it here. Hopefully noone will mind.

About interfaces and base classes:
A class can implent any number of interfaces, but only inherit from one base class.

A base class (abstract or not) can contain implentations of methods, variables (public, private and/or protected), constructors and so on, while interfaces can only provide a list of methods and/or properties the classes have to implent.


About events and delegates:
A delegate is somewhat similar to function pointers in C/C++, but they can be tied to an instance of any class making them very powerful. Basicly, a delegate is a variable type that allows you to assign a function/method to it, that you can call whenever you feel like.

An event is more of a hook. You can assign functions to it that'll be called when the declaring class feels like calling them. You can assign more than one method to an event. However, only the declaring class may raise the event and call the methods registered to it (not even inheriting classes may).


About commandline arguments:
If you want to pass commandline arguments without using a console, then right-click your project and click settings, then go to the Debug tab. There you can can enter commandline arguments that'll be passed to your app when you run it using F5. This is assuming you are using Visual C# 2005.
One more thing on interfaces. Am I correct in thinking that all methods in an interface are public and virtual ?

I just want to get used to key terms of the language and start using them more so they stick. This way I can more easily think in a C# way so to speak.
Someone asked about compiling a file from a command prompt.

If you have XNA installed for vc#e, you can go to:

start > xna game studio exp > tools > xna game studio express command prompt and use the csc command to compile your code there.

This topic is closed to new replies.

Advertisement