public variables vs properties

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What is better to use to reduce the amount of memory a class consume from
another class?
--
 
"Kenny M." <[email protected]> a écrit dans le message de
news: (e-mail address removed)...

| What is better to use to reduce the amount of memory a class consume from
| another class?

Memory at this level really isn't as important as maintaining the integrity
of your data. You should *never* allow public fields; always use properties
with a backing field. Don't forget, that anything apart from fields (methods
and properties) only takes up memory once per class; fields take up one per
instance.

Joanna
 
What is better to use to reduce the amount of memory a class consume from
another class?

Properties don't increase the instance size of a class, so I suggest
you use them.


Mattias
 
What is better to use to reduce the amount of memory a class consume from
another class?

Even if properties turned out to use more memory, the difference is
likely to be insignificant.

The benefits you gain from the user of properties in terms of cotrol
of access to and manipulation of your data far override any drawbacks
in terms of memory usage in relation to public fields
 
There is absolutely no excuse for exposing public fields. The first law of
OOP is encapsulation. A public field can be changed without the knowlege of
the owner class. This can lead to disaster.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
I would second the previous responsives and provide
a simple real world advantage to using properties.

Being able to tell which classes have at least one
property that has changed can allow you to skip
records in bulk loads that don't need to be processed.
Can definitely improve performance.

private string _isDirty = false;
private string _myProperty = String.Empty;

public string MyProperty
{
string valueTrimmed = "";

get
{
return _myProperty;
}
set
{
valueTrimmed = value.Trim();
if (_myProperty != valueTrimmed)
{
_myProperty = valueTrimmed;
_isDirty = true;
}
}
}

public bool IsDirty
{
get { return _isDirty; }
set { _isDirty = value; }
}

--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp
 
Back
Top