[C#] Boxing when using Properties?
Page 1 of 1
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Fri, 16th Jul 2010 13:00    Post subject: [C#] Boxing when using Properties?
Let's say I have this class:

Code:
class WorldElement
{
    private Byte tile;

    public Byte Tile
    {
        get { return tile; }
        set { tile = value; }
    }

    public WorldElement(Byte _tile)
    {
        tile = _tile;
    }
}



And I access it later on with:
Code:
    Int32 a = 5+5;
    WorldElement worldElement = new WorldElement(5);
    worldElement.Tile = a;


Will boxing occur? Or will it be just a conversion from Int32 to Byte?

And can I use Byte where int (aka Int32) is expected? Smile

Or should I just go with Int32 all the time and shit on the less memory usage of a Byte?


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73194
Location: Ramat Gan, Israel 🇮🇱
PostPosted: Fri, 16th Jul 2010 13:48    Post subject:
Basically, the rule is, if you are going from a small one to a larger one (there is no data loss), the conversion is implicit. That is, this will work:

Code:
byte b = 12;

int a = b;


On the other hand, when there is a possibility of data loss, you must explicitly convert the variable. For example:

Code:
int a = 12;

//byte b = a; //Won't even compile.
byte b = (Byte) a; //Explicit conversion, will work.


This is the same behavior as C++ and Java (and any type-safe language).

A table for your convenience:
http://msdn.microsoft.com/en-us/library/y5b434w4.aspx

But really, there are very few reasons to use bytes these days aside from compatibility reasons. Wink


My IMDb Ratings | Fix NFOHump Cookies | Hide Users / Threads | Embedded Content (Videos/GIFs/Twitter/Reddit) | The Derps Collection

Death smiles at us all; all we can do is smile back.


Last edited by LeoNatan on Fri, 16th Jul 2010 13:50; edited 1 time in total
Back to top
garus
VIP Member



Posts: 34200

PostPosted: Fri, 16th Jul 2010 13:49    Post subject:
snip


Last edited by garus on Tue, 27th Aug 2024 21:27; edited 1 time in total
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73194
Location: Ramat Gan, Israel 🇮🇱
PostPosted: Fri, 16th Jul 2010 13:53    Post subject:
It should not compile at all, because data conversion is done in run time, while this will fail in compile time. Smile
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Fri, 16th Jul 2010 14:07    Post subject:
Int32 to Byte is not happening without conversion with (Byte)

But if you say that there is no usage to it, I'll drop the whole byte stuff Smile I thought that C# might internally handle the Byte as something like... let's say Int8 Smile

The thing that I was worrying about is, that, as far as I know, with Properties you can say

Int32 blabla = 100;
worldElement.Tile = blabla;

And it shouldn't return an error, as Tile is the property and will do any conversion if needed to Byte. But 100 is a int32 and small enough to fit in Byte. So my question was, will C# do a conversion from 100 (Int32) to 100 (Byte) or does it handle it internally already as an int (like Int8) Smile


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73194
Location: Ramat Gan, Israel 🇮🇱
PostPosted: Fri, 16th Jul 2010 14:13    Post subject:
It doesn't matter if you are sending to a property, a variable or as a function parameter, it won't work. It won't even compile.

Properties are translated to getter and setter functions in CIL anyway. Smile

BTW, why do you use Int32 and not int? int is Int32, short is Int16 and long is Int64.
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Fri, 16th Jul 2010 14:38    Post subject:
As in C# internally int = Int32, why shouldn't I? Smile It's like using bool instead of Boolean

It's not like in Java where bool can just be true or false and Bool is an Object that can be true false or null Laughing

Btw thanks! Will try to replace all Byte with Int32 and see how powerful the Visual Studio IDE is Smile (should help me to replace all occurences, right? Smile )


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73194
Location: Ramat Gan, Israel 🇮🇱
PostPosted: Fri, 16th Jul 2010 14:46    Post subject:
I am just saying, conversion from int to Int32 is done at any rate by the compiler when creating the CIL assembly. So why write such bloat as Int32 when they are the same (especially when they are the same)? Smile Do you also type Boolean instead of bool? Razz

I use var anyway nowadays Razz It is just so much faster to use typing all these types Laughing

bool in C# can't be null because it is a structure and not a class. If you need it to be nullable, you can use the bool? type, or Nullable<bool>, which are the same. Then you can ask b.HasValue and act accordingly. Wink
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group