Jump to content
  • Advertisement
Sign in to follow this  
blewisjr

[.net] First time I ever saw this in C# can someone explain

This topic is 3647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is more of a syntax question but this is the first time I have ever seen this after a few years of using C#. I noticed this in my pong project of my XNA book. public Rectangle? sourceRect; public SpriteToRender(........, Rectangle? setSourceRect, .....) What is the purpose of the '?' after the Rectangle class?

Share this post


Link to post
Share on other sites
Advertisement
Rectangle is a value type (a struct), meaning it can't be set to null. You can use the Nullable<T> class to allow null values. T? is just shorthand for Nullable<T>.
int x = null; // error
int? y = null; // allowed

/* ... */

if ( y.HasValue )
{
int yVal = y.Value;
/* ... */
}
So, SpriteToRender allows you to pass null for its setSourceRect argument. Without the ? that wouldn't have been possible, and they'd have to rely on overloading or something to offer a variant which works with default values.

More at Nullable types in C#

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!