Setting a Generic Type to Zero

J

Jonathan Wood

I've decided to write a little routine using generics. That is, a method
that takes an out argument of any data type.

However, being the quirky language that C# is, I have the following problem.

The method returns a bool value that indicates if it is successful. If it is
successful, the out argument's value is set. But C# requires that I set the
value of this argument even if I'm not successful.

In the case of an integer, I would just set it to zero. But how can I do
similarly for all the other data types a generic method might be passed?

Thanks.
 
J

jehugaleahsa

I've decided to write a little routine using generics. That is, a method
that takes an out argument of any data type.

However, being the quirky language that C# is, I have the following problem.

The method returns a bool value that indicates if it is successful. If it is
successful, the out argument's value is set. But C# requires that I set the
value of this argument even if I'm not successful.

In the case of an integer, I would just set it to zero. But how can I do
similarly for all the other data types a generic method might be passed?

Thanks.

default(T). It's magic keyword that returns the default value for any
type. (0, 0.0, false or null).
 
J

Jonathan Wood

Cool! I was wondering about some sort of special syntax. That's it.

Thanks!

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

I've decided to write a little routine using generics. That is, a method
that takes an out argument of any data type.

However, being the quirky language that C# is, I have the following
problem.

The method returns a bool value that indicates if it is successful. If it
is
successful, the out argument's value is set. But C# requires that I set
the
value of this argument even if I'm not successful.

In the case of an integer, I would just set it to zero. But how can I do
similarly for all the other data types a generic method might be passed?

Thanks.

default(T). It's magic keyword that returns the default value for any
type. (0, 0.0, false or null).
 

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