Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Imgur: The most awesome images on the Internet[^]

With that image, Am I thinking wrong or is that true?

What I have tried:

When writing property, which way is more correct?
Posted
Updated 2-Mar-16 7:59am
Comments
Philippe Mori 2-Mar-16 23:51pm    
Why you don't write code here?
Umut Comlekcioglu 3-Mar-16 10:37am    
Just beacuse, I think, I can explain what I want with image. Otherwise, there is no different with image(I'm write code on image) or writing code on here...
Philippe Mori 3-Mar-16 11:02am    
This is not how the site is intended to be used. A question should always have basic information dirtectly embedded in it.

By not using the site correctly, you get a lot of vote of 1 which might affect your reputation.
Umut Comlekcioglu 3-Mar-16 12:15pm    
I didn't think like that. I'll be more careful
johannesnestler 3-Mar-16 7:34am    
I think you have to learn the basics about the inner workings of .NET first, before you can seriously think about "memory" optimization tricks. The good thing in .NET - you normally don't have to. Don't get me wrong, I assume you come from another prgramming domain where such things are/were important (C++?), and therefore you ask this. If you really want to "know" I would suggest you try to experiment with different code and then look at the intermediate language output. e.g properties will generate methods (set/get) and use vtable memory and so on.. - so my point is: If want to "know" about your (custom) .NET memory Management - you have to dive into a lot of (mostly academic) concepts - and .NET was build to free your mind from these... So if you really need to "know" then better do your Project in another language, or write unsafe (-compiled) .NET-code where you control memory for your self. That's the reason I don't see any value in your question - and to find the "correct" style in an "offer" of different grades of syntactic sugar, has nothing to do with memory or perfomance... (you understand that "Properties" are just syntactic sugar in the first place to avoid writing and calling all These get and set functions we used to do in C++)... So code like this:
using System;

namespace PropertyMemoryTest
{
class Program
{
static void Main(string[] args)
{
var withproperty = new WithProperty();
Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(withproperty));
var variable = new WithVariable();
Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(variable));
Console.ReadKey(true);
}
}

struct WithProperty
{
public int Property { get; set; }
}

struct WithVariable
{
public int Variable;
}
}
Won't tell you which is "better" from (total)memory consumption/perfomance , but I think you will agree that the version with property has much more complex IL code. So get over "Micro-optimization" thinking, and think about other thins like maintenance or extensibillity - you will always go for the property vs. "the variable", even if that means double memory consumption (calculate modern RAM cost to the cost an work hour)... jus my 2 c - happy coding!

1 solution

Next time, just put the code in the post, not in an image.

No, you're wrong about the "8 bytes".

The top code snippet in the image is just a shortcut, an "auto-implemented" property. Behind the scenes it's doing exactly the same thing as the second code snippet in the image.

In both cases, the allocation is just a 32-bit integer, 4 bytes to store the value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900