C# FAQ: How specify a CSharp literal type

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

How specify a C# literal type?

Sometimes, it is necessary to notify the C# compiler that a literal must be treated as a certain number type. This can be accomplished by adding a number type suffix to the end of the literal. For example:

1m;  // System.Decimal 
1f;  // System.Single 
1d;  // System.Double 
1U;  // unsigned int
1ul; // unsigned long 

This is a useful feature when a literal must be matched to a signature or a value is specified in order to defeat an undesired implicit cast of 0.5 to a double. For instance:

Hashtable table = new Hashtable (1000, 0.5);

The preceding line will not compile; because, the Hashtable constructor expects parameters (int, float); but, the preceding is (int, double). To defeat the implicit cast, use a line similar to the following:

Hashtable table = new Hashtable (1000, 0.5f);

A complete listing of the suffixes is specified in the C# Language Specifcation sections specified below.

See also



Personal tools