473,401 Members | 2,068 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,401 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 10948
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.