473,385 Members | 1,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Method Signature For A Method That Can Return Objects and PrimitiveData Types...

I'm a Java developer in the process of writing a class library in C#,
so please bear with me.

I'm trying to write a method that can return a generic Object OR a
Decimal value. In Java I would just have the method return an Object,
since all Java primitive data types have Object wrappers like Integer
and Double.

If I have a method signature that returns Object in C#, can I return a
Decimal value within the method body? I was looking online at it
appears that Decimal is a Struct, not a Class, so I'm not sure if this
is legal.

Thanks for help with the solution.

The Sunburned Surveyor
Dec 20 '07 #1
15 1230
On Thu, 20 Dec 2007 09:57:20 -0800, Sunburned Surveyor
<su****************@gmail.comwrote:
[...]
If I have a method signature that returns Object in C#, can I return a
Decimal value within the method body? I was looking online at it
appears that Decimal is a Struct, not a Class, so I'm not sure if this
is legal.
It is.

You may want to read up on "boxing", as returning a value type as an
"object" will force the value type to be wrapped in a reference type
instance (boxed). Casting back to decimal will unbox it, allowing it to
be assigned to a variable of the correct value type (decimal in this case,
of course).

Of course, you could have just tried it to see if it works. :)

Pete
Dec 20 '07 #2

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 20 Dec 2007 09:57:20 -0800, Sunburned Surveyor
<su****************@gmail.comwrote:
>[...]
If I have a method signature that returns Object in C#, can I return a
Decimal value within the method body? I was looking online at it
appears that Decimal is a Struct, not a Class, so I'm not sure if this
is legal.

It is.

You may want to read up on "boxing", as returning a value type as an
"object" will force the value type to be wrapped in a reference type
instance (boxed). Casting back to decimal will unbox it, allowing it to
be assigned to a variable of the correct value type (decimal in this case,
of course).

Of course, you could have just tried it to see if it works. :)

Pete

An addendum to Pete's excellent advice: "boxing" is very slow.

If you intend to call this method very often, you should seriously consider
simply creating a new method.

Another option would be to use overloaded methods with an "out" parameter
instead of a function result. This assumes, of course, that you know the
type of the "result" before you call the method.

If I had to guess, I'd say you're writing some sort of "WinAPI"-ish function
which returns an object if it succeeds and an error code if it fails. In
that case, you might consider always returning a decimal (0 = success) and
using an "out" parameter for your success "result".

Dec 20 '07 #3
On Dec 20, 12:07 pm, "Scott Roberts" <srobe...@no.spam.here-webworks-
software.comwrote:
"Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote in message

news:op***************@petes-computer.local...


On Thu, 20 Dec 2007 09:57:20 -0800, Sunburned Surveyor
<sunburned.surve...@gmail.comwrote:
[...]
If I have a method signature that returns Object in C#, can I return a
Decimal value within the method body? I was looking online at it
appears that Decimal is a Struct, not a Class, so I'm not sure if this
is legal.
It is.
You may want to read up on "boxing", as returning a value type as an
"object" will force the value type to be wrapped in a reference type
instance (boxed). Casting back to decimal will unbox it, allowing it to
be assigned to a variable of the correct value type (decimal in this case,
of course).
Of course, you could have just tried it to see if it works. :)
Pete

An addendum to Pete's excellent advice: "boxing" is very slow.

If you intend to call this method very often, you should seriously consider
simply creating a new method.

Another option would be to use overloaded methods with an "out" parameter
instead of a function result. This assumes, of course, that you know the
type of the "result" before you call the method.

If I had to guess, I'd say you're writing some sort of "WinAPI"-ish function
which returns an object if it succeeds and an error code if it fails. In
that case, you might consider always returning a decimal (0 = success) and
using an "out" parameter for your success "result".- Hide quoted text -

- Show quoted text -
Thanks for the responses Scott an Peter. I'm actually defining the
method as part of an interface. In most cases the implementations of
this interface will simply return a Decimal value from this particular
method. However, the occassional implementation of this interface may
want to return a custom object from this method.

It sounds like I may need to just provide a method for the custom
object access.

Thanks again for the help.

The Sunburned Surveyor

Dec 20 '07 #4
Scott Roberts <sr******@no.spam.here-webworks-software.comwrote:

<snip>
An addendum to Pete's excellent advice: "boxing" is very slow.
"Very slow"? It's actually really, really fast - unless you're using it
millions of times, of course. It's not free, and should be carefully
considered, but I don't think I'd really call it "very slow".
If you intend to call this method very often, you should seriously consider
simply creating a new method.
Again, it depends on what you call "very often". In a microbenchmark
(with all the caveats they always involve) I can box an integer about
100 million times in a single second on my laptop.

So, if you're "only" going to call the method a few thousand times a
second, I don't think the performance penalty is likely to be
significant.

<snip>
If I had to guess, I'd say you're writing some sort of "WinAPI"-ish function
which returns an object if it succeeds and an error code if it fails. In
that case, you might consider always returning a decimal (0 = success) and
using an "out" parameter for your success "result".
Or preferably use an exception for failure cases.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 20 '07 #5

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Scott Roberts <sr******@no.spam.here-webworks-software.comwrote:

<snip>
>An addendum to Pete's excellent advice: "boxing" is very slow.

"Very slow"? It's actually really, really fast - unless you're using it
millions of times, of course. It's not free, and should be carefully
considered, but I don't think I'd really call it "very slow".
>If you intend to call this method very often, you should seriously
consider
simply creating a new method.

Again, it depends on what you call "very often". In a microbenchmark
(with all the caveats they always involve) I can box an integer about
100 million times in a single second on my laptop.

So, if you're "only" going to call the method a few thousand times a
second, I don't think the performance penalty is likely to be
significant.
I'll defer to your expertise, but we used boxing extensively in our OPF and
saw marked improvement when we switched to native types.
>If I had to guess, I'd say you're writing some sort of "WinAPI"-ish
function
which returns an object if it succeeds and an error code if it fails. In
that case, you might consider always returning a decimal (0 = success)
and
using an "out" parameter for your success "result".

Or preferably use an exception for failure cases.
I can say for certain that exceptions are "very slow". ;)

Dec 21 '07 #6
Scott Roberts wrote:
I can say for certain that exceptions are "very slow". ;)
Exceptions are for exceptional situations - they can not
happen frequently.

Arne
Dec 21 '07 #7

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:47***********************@news.sunsite.dk...
Scott Roberts wrote:
>I can say for certain that exceptions are "very slow". ;)

Exceptions are for exceptional situations - they can not
happen frequently.

Arne
Exactly my point. If "failure" happens frequently, then exceptions are not
the answer.

Dec 21 '07 #8
Scott Roberts <sr******@no.spam.here-webworks-software.comwrote:
So, if you're "only" going to call the method a few thousand times a
second, I don't think the performance penalty is likely to be
significant.

I'll defer to your expertise, but we used boxing extensively in our OPF and
saw marked improvement when we switched to native types.
Then I suspect you were in the case of using them hundreds of thousands
of times a second.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 21 '07 #9
Scott Roberts <sr******@no.spam.here-webworks-software.comwrote:

<snip>
Or preferably use an exception for failure cases.

I can say for certain that exceptions are "very slow". ;)
When used properly, they are not a performance issue. You'll see a
performance hit if you throw hundreds of thousands of exceptions, but
they're not nearly as bad as people make them out to be.

The way people talk about them, you'd think they took a second or so to
throw.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 21 '07 #10

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Scott Roberts <sr******@no.spam.here-webworks-software.comwrote:

<snip>
Or preferably use an exception for failure cases.

I can say for certain that exceptions are "very slow". ;)

When used properly, they are not a performance issue. You'll see a
performance hit if you throw hundreds of thousands of exceptions, but
they're not nearly as bad as people make them out to be.

The way people talk about them, you'd think they took a second or so to
throw.
If you throw hundreds of thousands of them your application will come
crashing to its knees. We have a process that rates telephone calls. We rate
batches containing several million at a time. There are many, many, many
things that can be "wrong" with a record that prevents it from being rated
properly. My first iteration used exceptions when something went wrong. It
was unusable. I switched to "error codes" and now it takes a few minutes.
"Performance hit" is putting it mildly.

I'm not sure how my simple comment to a new .net developer (coming from the
world of Java) that boxing is less performant than native types turned into
a discussion about the "proper" use of exceptions. I even qualified my
observation by saying that it would only be an issue if the method were
called very frequently. I tried to draw a conclusion (use of return codes)
from the OP's question, assumed there was a reason for it (using return
codes), and attempted to offer a solution. Sorry if that pushed one of your
buttons.

Dec 21 '07 #11
Scott Roberts <sr******@no.spam.here-webworks-software.comwrote:
The way people talk about them, you'd think they took a second or so to
throw.

If you throw hundreds of thousands of them your application will come
crashing to its knees.
Well, it will take tens of seconds to throw them.
We have a process that rates telephone calls. We rate
batches containing several million at a time. There are many, many, many
things that can be "wrong" with a record that prevents it from being rated
properly. My first iteration used exceptions when something went wrong. It
was unusable. I switched to "error codes" and now it takes a few minutes.
"Performance hit" is putting it mildly.
In a situation where it's reasonably expected for records to be
"wrong", yes. That's the ideal time *not* to use exceptions. That's why
I said they're not a performance issue when used properly.
I'm not sure how my simple comment to a new .net developer (coming from the
world of Java) that boxing is less performant than native types turned into
a discussion about the "proper" use of exceptions.
You didn't say that boxing is "less performant than native types". You
said it was "very slow". There's a massive difference between those
statements. The first is true. The second is an overstatement, in my
view.
I even qualified my observation by saying that it would only be an
issue if the method were called very frequently.
Well, again that's not *quite* what you said - but it certainly gave
the wrong impression, IMO.
I tried to draw a conclusion (use of return codes)
from the OP's question, assumed there was a reason for it (using return
codes), and attempted to offer a solution. Sorry if that pushed one of your
buttons.
People often bend perfectly reasonable designs out of shape to avoid
boxing and exceptions precisely because of claims that they are "very
slow" when actually they're only going to call the relevant routine a
few thousand times an hour.

In one discussion, people seriously claimed that a few hundred
exceptions being thrown in an hour would be a significant performance
hit for a web application. See how these things get out of control?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 21 '07 #12
In one discussion, people seriously claimed that a few hundred
exceptions being thrown in an hour would be a significant performance
hit for a web application. See how these things get out of control?
I'm not going to argue semantics with you. Suffice it to say, this was not
one of those discussions.

Dec 21 '07 #13
Scott Roberts <sr******@no.spam.here-webworks-software.comwrote:
In one discussion, people seriously claimed that a few hundred
exceptions being thrown in an hour would be a significant performance
hit for a web application. See how these things get out of control?

I'm not going to argue semantics with you. Suffice it to say, this was not
one of those discussions.
I never said it was - merely that people take away impressions from
phrases like "very slow" which *lead* to discussions like that.

Do you really not appreciate that there's a significant difference
between "less performant than native types" and "very slow"? It's not
just splitting hairs, it's a definite difference which could easily
influence people in their coding when it shouldn't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 21 '07 #14

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Scott Roberts <sr******@no.spam.here-webworks-software.comwrote:
In one discussion, people seriously claimed that a few hundred
exceptions being thrown in an hour would be a significant performance
hit for a web application. See how these things get out of control?

I'm not going to argue semantics with you. Suffice it to say, this was
not
one of those discussions.

I never said it was - merely that people take away impressions from
phrases like "very slow" which *lead* to discussions like that.

Do you really not appreciate that there's a significant difference
between "less performant than native types" and "very slow"? It's not
just splitting hairs, it's a definite difference which could easily
influence people in their coding when it shouldn't.
It could also influence people in their coding when it should. The fact of
the matter is, you didn't bother to find out if boxing and/or exceptions
were "proper" for the OPs request, you just said he should do it. I inferred
that if he were using return codes in the first place that there was a
reason (frequent "errors"). I think that your recommending these techniques
without bothering to mention the performance drawbacks is, quite frankly,
irresponsible on your part. These kinds of blanket recommendations *lead* to
programmers all over the world thinking .Net is "very slow".

And since you ask, I do think that 20 times longer qualifies as "very slow".
That's just my opinion and you are unlikely to change it.

"Boxing and unboxing are computationally expensive processes. When a value
type is boxed, an entirely new object must be created. This can take up to
20 times longer than an assignment. When unboxing, the casting process can
take four times as long as an assignment."

http://msdn2.microsoft.com/en-us/lib...96(VS.80).aspx

Dec 21 '07 #15
Scott Roberts <sr******@no.spam.here-webworks-software.comwrote:
Do you really not appreciate that there's a significant difference
between "less performant than native types" and "very slow"? It's not
just splitting hairs, it's a definite difference which could easily
influence people in their coding when it shouldn't.

It could also influence people in their coding when it should. The fact of
the matter is, you didn't bother to find out if boxing and/or exceptions
were "proper" for the OPs request, you just said he should do it. I inferred
that if he were using return codes in the first place that there was a
reason (frequent "errors"). I think that your recommending these techniques
without bothering to mention the performance drawbacks is, quite frankly,
irresponsible on your part. These kinds of blanket recommendations *lead* to
programmers all over the world thinking .Net is "very slow".
I don't think very many programs have actually been slow due to boxing
being a bottleneck, to be honest.
And since you ask, I do think that 20 times longer qualifies as "very slow".
That's just my opinion and you are unlikely to change it.
I suspect not. Then again, you're unlikely to change my opinion,
either. Boxing is very, very rarely a bottleneck in my experience.
"Boxing and unboxing are computationally expensive processes. When a value
type is boxed, an entirely new object must be created. This can take up to
20 times longer than an assignment. When unboxing, the casting process can
take four times as long as an assignment."
Wow, 20 times longer than an assignment. Assignments are *blazingly*
quick - it doesn't make boxing "very slow", just *relatively* slow.
Yes, it's slower than a straight assignment. Making a method call which
isn't inlined is much slower than making a call which *is* inlined -
but that doesn't make non-inlined method calls "very slow" either.

Yes, it's worth being aware of the penalty of boxing - but I've seen
far more code which is badly written due to micro-optimisation than I
have code which performs unacceptably due to boxing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 21 '07 #16

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

Similar topics

31
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would...
7
by: Marcos Stefanakopolus | last post by:
So this is ok: class foo { int myMethod(string, int) { ... } int myMethod(string, double) { ... } } But this is not: class foo {
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
6
by: LordHog | last post by:
Hello all, I recently ran into a strange behavior which I don't understand. I have two 'Add' method which a slightly different signature which they look like public void Add( string varName,...
2
by: John | last post by:
Hi, I am implementing web services and try to return a collection of records, say 1,"John","Doe",1/1/1990 2,"Jane","Doe",2/1/1990 3,"Joe","Smith",3/1/1990 .......
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
2
by: John Sitka | last post by:
Hi, I'm building a simple "look up" web method that would return a table with 9 columns and a dynamic number of rows, then I find out datatables cannot be serialized. Not a big deal the .Net...
9
dmjpro
by: dmjpro | last post by:
************************** Sorry the title would be Overriding. Please experts, correct the title ;) ************************** Method signature includes ... 1.Modifiers 2.Return type...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.