471,319 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Type Literals

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
Dec 19 '05 #1
4 10706
Tyler Durden <Ty*********@discussions.microsoft.com> wrote:
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;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 19 '05 #2
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?

"Jon Skeet [C# MVP]" wrote:
Tyler Durden <Ty*********@discussions.microsoft.com> wrote:
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;

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

Dec 19 '05 #3
Tyler Durden wrote:
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

Dec 20 '05 #4
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.
"Michael Petrotta" wrote:
Tyler Durden wrote:
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

Dec 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tao Lu | last post: by
reply views Thread by Robin Tucker | last post: by
6 posts views Thread by copx | last post: by
15 posts views Thread by George Sakkis | last post: by
8 posts views Thread by minseokoh | last post: by
4 posts views Thread by Ben Petering | last post: by
7 posts views Thread by lithiumcat | last post: by
15 posts views Thread by s0suk3 | last post: by
reply views Thread by rosydwin | last post: by

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.