473,382 Members | 1,442 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,382 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 10945
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"; }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.