473,569 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instatiating an object in the get property method

Is it bad form to instatiate a private variable in the get method of a
property declaration?

ie)

private objA _someVar;

public objA SomeVar
{
get { if( _someVar == null) _someVar = new objA();
return _someVar;
}
}
Nov 16 '05 #1
6 1068
Jack,

No, this is not a bad idea at all. It's referred to as "lazy-loading"
and is done when the resource is expensive to create.

The only problem with this is that it is not thread safe. If this is
the case, then you will have to use a lock statement (I recommend the double
check lock pattern) to create the instance if it is not created already.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Jack Addington" <ja********@sha w.ca> wrote in message
news:OI******** ********@tk2msf tngp13.phx.gbl. ..
Is it bad form to instatiate a private variable in the get method of a
property declaration?

ie)

private objA _someVar;

public objA SomeVar
{
get { if( _someVar == null) _someVar = new objA();
return _someVar;
}
}

Nov 16 '05 #2
Hi, Jack.
As long as the design is solid, I don't see any systematic problem with
this technic. For example, it could be used to perform a delayed instantiate
a singleton object.

Ming Chen

"Jack Addington" <ja********@sha w.ca> wrote in message
news:OI******** ********@tk2msf tngp13.phx.gbl. ..
Is it bad form to instatiate a private variable in the get method of a
property declaration?

ie)

private objA _someVar;

public objA SomeVar
{
get { if( _someVar == null) _someVar = new objA();
return _someVar;
}
}

Nov 16 '05 #3
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard .caspershouse.c om> wrote:
No, this is not a bad idea at all. It's referred to as "lazy-loading"
and is done when the resource is expensive to create.

The only problem with this is that it is not thread safe. If this is
the case, then you will have to use a lock statement (I recommend the double
check lock pattern) to create the instance if it is not created already.


The double-check lock pattern doesn't work in .NET unless the variable
is also volatile. There's no guarantee that the object will have
completed being constructed before the value in the variable is set to
the new reference, for example.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Daniel Jin <Da*******@disc ussions.microso ft.com> wrote:
but if you are running the code on a x86 machine, double locking should be
ok, correct? though it could potentially be ported to a system with very
weak memory model.


I don't know whether it's definitely correct, even on x86. It may well
be, but I certainly wouldn't like to write a load of code assuming it.
You never know where your code will end up...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
On Tue, 21 Sep 2004 18:40:03 +0100, Jon Skeet [C# MVP]
<sk***@pobox.co m> wrote:
Daniel Jin <Da*******@disc ussions.microso ft.com> wrote:
but if you are running the code on a x86 machine, double locking should be
ok, correct? though it could potentially be ported to a system with very
weak memory model.


I don't know whether it's definitely correct, even on x86. It may well
be, but I certainly wouldn't like to write a load of code assuming it.
You never know where your code will end up...


CLI I 12.6.8 says:

"It is explicitly not a requirement that a conforming implementation
of the CLI guarantee that all state updates performed within a
constructor be uniformly visible before the constructor completes.
CIL generators may ensure this requirement themselves by inserting
appropriate calls to the memory barrier or volatile write
instructions. "

When I read this it tends to make me want to err on the safe side and
avoid the situation altogether. You never know what conforming
implementation your code might end up on...

--
Scott
http://www.OdeToCode.com
Nov 16 '05 #6
but if you are running the code on a x86 machine, double locking should be
ok, correct? though it could potentially be ported to a system with very
weak memory model.

"Jon Skeet [C# MVP]" wrote:
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard .caspershouse.c om> wrote:
No, this is not a bad idea at all. It's referred to as "lazy-loading"
and is done when the resource is expensive to create.

The only problem with this is that it is not thread safe. If this is
the case, then you will have to use a lock statement (I recommend the double
check lock pattern) to create the instance if it is not created already.


The double-check lock pattern doesn't work in .NET unless the variable
is also volatile. There's no guarantee that the object will have
completed being constructed before the value in the variable is set to
the new reference, for example.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
4527
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " + salaryMinLabel.value); } I also have an asp.net object:
5
14415
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with the class and this works but I would also like to know of other ways to do this. Also are there any performance implacations of using sealed?
3
2759
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Defining_Functions doesn't actually give any insight.
2
2631
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.