what is a 'Sealed partial class'

Started by
5 comments, last by NEXUSKill 13 years, 2 months ago
As i am implementing the FPS game component, i have added a blank GameComponent to my project, but its definition is slightly different to the one in the book.


public sealed partial class FPS : Microsoft.Xna.Framework.DrawableGameComponent


as opposed to the one that Visual studio creates for me:


public class FPS : Microsoft.Xna.Framework.DrawableGameComponent


The classes methods are also marked as sealed, Eg

public sealed override void Draw(GameTime gameTime) { }


So my question is what exactly is a "sealed partial class"?


Cheers all
Advertisement
Partial keyword: http://msdn.microsof...488(VS.80).aspx
It is possible to split the definition of a class or a struct, or an interface over two or more source files.
Each source file contains a section of the class definition, and all parts are combined when the application is compiled.[/quote]

Sealed keyword: http://msdn.microsof...w(v=VS.80).aspx
The sealed modifier can be applied to classes, instance methods and properties. A sealed class cannot be inherited.
A sealed method overrides a method in a base class, but itself cannot be overridden further in any derived class.[/quote]
Visual studio tends to make a separate file for its auto-gen class definitions. Quite often there's part not intended for a human to alter the code for (where you use a designer) and another separate file for placing the user code.

For example in WinForms the GUI designer is used to place controls and that goes into the design portion of class (in a separate cs file) and the event hookups for the click event etc go into the user portion cs file.
An important observation when overriding virtual methods is that if you mark it as sealed, it will cost less to use it, so an override that wont be overriden any further should be marked as sealed.

For instance, the Update and Draw calls of game, once you have your own game derivate class, these methods should be marked sealed, since you are not likely to create yet another derivate.

This also serves a certain security purpose, no one can link your library and override methods you did not intended to be overriden.

It is also a good idea to mark these final classes and methods as internal.

Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272


okay i think i get it :) thanks.

How come it costs less if you mark an overridden virtual method as sealed? what is the difference, when its all compiled and its obviously not going to be overridden anymore wouldnt the compiler just treat a sealed and non-sealed method the same?
Here is one explanation from http://dotnetperls.com/sealed

Applying the sealed keyword in the C# programming language tells the C# compiler to apply the "sealed" metadata decoration in the assembly of your class. The sealed keyword is a syntax hint that restricts class declarations that might build on top of your sealed class. Additionally, the JIT compiler can detect the sealed decoration and optimize method invocations better when it is used...

Kinda off topic here, but if you want to learn about other optimizations specific to game development in C# / XNA, these videos were very informative:

Microsoft's PDC '10 featured two great talks from Microsoft pros about Windows Phone 7 and games, focused on getting the most out of Windows Phone 7 to turn game ideas into fully-featured, high-performance experiences. Tune into these talks, available on-demand with video and downloadable decks.


Things You Need to Know Before Building XNA Games for Windows Phone 7 - Shawn Hargreaves
(http://player.microsoftpdc.com/Session/b8100382-1fdf-482e-b4ec-2b1f0315987f)

So you have a cool idea for a game. This session covers some of the less obvious things you will need to know to turn your idea into reality when using XNA Game Studio for Windows Phone. Topics include how to choose the best orientation and resolution, how game content differs from typical productivity or web applications, how to deal with tombstoning, how to speed up load times, and how to proceed if you are interested in getting access to Xbox LIVE on the phone.

Real-World Analysis and Optimization of XNA Framework Games on Windows Phone 7 - Jeff Petkau
(http://player.microsoftpdc.com/Session/6a4f4c01-5984-4b33-9e27-e725791980b1)

Good code design, performance tips, and a solid understanding of the platform are all essential to game development. Learn how to use the Microsoft Advanced Technology Group's (ATG) battle-tested techniques to find and fix performance-killing issues in Windows Phone 7 XNA Framework game code. Also, hear about real-world analysis and optimization of XNA Framework games on Windows Phone 7.

[/quote]


okay i think i get it :) thanks.

How come it costs less if you mark an overridden virtual method as sealed? what is the difference, when its all compiled and its obviously not going to be overridden anymore wouldnt the compiler just treat a sealed and non-sealed method the same?


If a method is public virtual, and the owner class is also public, the compiler does not know if the method won't be overridden by another library, so it "leaves the door open".


Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272


This topic is closed to new replies.

Advertisement