473,320 Members | 2,111 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,320 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 10940
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tao Lu | last post by:
class A { pub: A(const char*); //1 A(char *); //2 } ; if i do A a("blah"); which constructor should be called?
12
by: Charlie Zender | last post by:
Hi, I am unable to compile a large body of code with extremely pedantic compile time checks activate, so that warnings cause errors. With GCC 3.3.1, I do this with gcc -std=c99 -pedantic...
0
by: Robin Tucker | last post by:
I'm localizing and need to find all string literals in my source code. I don't want to find any of the literals that will end up in resources however. As I don't use the "Me" keyword, I know...
6
by: copx | last post by:
Can you / are you supposed to free() string literals which are no longer needed? In my case I've menu construction code that looks like this: menu_items = list_new(); list_add(menu_items,...
15
by: George Sakkis | last post by:
Although I consider dict(**kwds) as one of the few unfortunate design choices in python since it prevents the future addition of useful keyword arguments (e.g a default value or an orderby...
8
by: minseokoh | last post by:
Hi, Could someone explain why "const" is located after the function name and what it means? inline unsigned char *access(int off) const { if (off < 0) abort(); return (&bits_); }
4
by: Ben Petering | last post by:
Hi group, this is a 'best practice' type question (I want discussion of the issue - whys and why nots - similar to "casting the return value of malloc()", to cite an analogous case). Let's...
7
by: lithiumcat | last post by:
Hi, I'm not yet very confident in my use of standard terminology, so please be kind if I'm mis-calling something, I will do my best no to make it again once pointed out. I'm wondering what is...
15
by: s0suk3 | last post by:
Hi, I've heard that a string literal has type 'char *' (i.e., a pointer to a char object). But I'm confused as to why this works: char *GetString(void) { return "hello"; }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.