objects as return values

B

BAZRAMIT

Many hii's,

Going back to C++ days, one could not safely use objects as return values.
In odt net, i freely return any object, how is it working in dot net?
 
W

William Ryan

I'm not sure what you are asking. You couldn't return
objects as return values safely?

Do you mean in seperate threads? If not, I think you may
be mistaken about your first statement..and even then.

You can definitely return objects in .NET, as a matter of
fact, have fun trying to avoid it ;-) . There's a big
difference between value and reference types, but I don't
know if that's your question or not. If not, let me know
a little more of the details and hopefully I can help you
out.

Cheers,

Bill
 
R

Ron McNulty

You may be overlooking the automatic garbage collection in C#. In C++ the
caller had to know explicitly if it was their responsibility (or not) to
dispose of the returned object. In C# it simply does not matter.

Regards

Ron
 
B

BAZRAMIT

I am sorry I could not make my question clear enough.

My question is:

in C++, it is not safe to return value objects from functions :

Object GetThatObject()
{
return new Objetc() ;
}

So we were returning register-sized values or references only.
But using dot net, I did not experience any problems in returning object
values!

Is this true?
 
J

Jerry III

Your example will not even compile (new returns a pointer but your function
is supposed to return an object). That may explain why you think it's not
safe to return objects, you probably have no understanding of the underlying
technology (what objects are, how they're created and destroyed and so on).
That's why GC languages are so popular these days, you don't have to worry
about memory allocation at the cost of speed and inefficiency. The short
answer is yes, you can go ahead and return as many objects as you want.

Jerry
 
S

Stu Smith

Well, you do still have to worry about memory allocation/deallocation, just
not as much as you perhaps used to with C++.

Not worrying about it is a recipe for a slow/bloated/buggy application.

Stu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top