Type Literals

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

Guest

Hi:

Just wondering if anyone can point me at some documentation for specifying
literals with type in C#. I have searched for this a couple of times with no
luck (found the VB.NET equivalent). I've tried google, msdn, etc, etc.
Obviously I must be using the wrong keywords or something.

I want to find documentation that describes how to do the following for all
of the base types:
decimal unitPrice = 2.5M;

Where are all of these defined or what are the keywords etc to find this in
the help?

Thanks,
Mark
 
Tyler Durden said:
Just wondering if anyone can point me at some documentation for specifying
literals with type in C#. I have searched for this a couple of times with no
luck (found the VB.NET equivalent). I've tried google, msdn, etc, etc.
Obviously I must be using the wrong keywords or something.

I want to find documentation that describes how to do the following for all
of the base types:
decimal unitPrice = 2.5M;

Well, there's the C# spec - here's a link to the literals page, where
you can follow links to the various types:

http://www.jaggersoft.com/csharp_standard/9.4.4.htm

However, if you're not bothered about the docs themselves, and just
want to know what's available:

Floating point literals:
float f = 1.2F; // or 1.2f;
double d = 1.2D; // or 1.2d;
decimal m = 1.2M; // or 1.2m;

(You can also use exponents when specifying floating point literals.)

Integer literals:
uint u = 1U; // or 1u;
long l = 1L; // or 1l;
ulong ul = 1UL; // or 1ul;
 
Thanks Jon, that was very helpful.

I was surprised however that there does not seem to be a literal for byte
values. The compiler, as an example, would not compile

byteValue = condition ? 0 : 1;

and so I had to change it to (after looking for this documentation on
literal types):

byteValue = condition ? Convert.ToByte(0) : Convert.ToByte(1);

So I wonder if the lack of a byte literal was an oversite or if there is a
much neater way to this?
 
Tyler said:
Thanks Jon, that was very helpful.

I was surprised however that there does not seem to be a literal for byte
values. The compiler, as an example, would not compile

byteValue = condition ? 0 : 1;

and so I had to change it to (after looking for this documentation on
literal types):

byteValue = condition ? Convert.ToByte(0) : Convert.ToByte(1);

So I wonder if the lack of a byte literal was an oversite or if there is a
much neater way to this?

Well, for what it's worth, you could cast it:
byteValue = condition ? (byte)0 : (byte)1

Make sure your casts will always succeed, of course.

Michael
 
Thanks Michael,

Yeah I have also used that method, but I was hoping for something that would
be evaluated at compile time rather than runtime. I have implemented a
solution and shipped the code, it was more for future reference that I was
hoping there may have been some syntax that would force compile time
evaluation of the code.

Thanks anyway for you efforts and hopefully in CS3.0 they may include more
complete literals for all of the base types.
 

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

Back
Top