1. Home
  2. Computing & Technology
  3. C / C++ / C#

C# Tutorial - Learn the Fundamentals of Programming

By , About.com Guide

6 of 10

Using Numeric Suffixes and Bool Types

Numeric Suffixes

This line doesn't compile in C#.
float fred=9.0;
That's because the default type of a floating point numeric literal - the characters 9.0 is double not float. But a double is twice the size of a float and cannot be stored in a float variable and so the compiler generates a compile error.

To make this compile you either have to cast it to force the issue, which is not very good or add the suffix f (or F).

float fred=9.0f;
The compiler makes assumptions about the type of number it determines from the literal text. If it lacks a decimal point then the compiler treats it as an integer. It then chooses the most appropriate size from int, uint, long or ulong. It will though implicitly convert ints to byte, sbyte, short and ushort and float or doubles. So this is ok.
float fred=9;

Integer Suffixes

Without a suffix, the compiler chooses int, uint, long, ulong according to the size of number. You can also use the suffix letters U,u, l or L. U and u mean unsigned, L or l means long. Any 2 letter combination of these letters UL, ul, UL,Ul is ok.

Floating Point Suffixes Double doesn't have a suffix. Use f or F for float and M or m for decimal.

Bool Types

Just like in Pascal and C++, C# supports a bool type that takes the values true or false. The result of an expression is a bool and you can assign an expression to a bool variable. You don't need brackets around an expression- except in an if statement as in
if ( EndLevel == IsRaised )
   DoSomething;
bool endOfRun = EndLevel == IsRaised;
bool endOfRun = ( EndLevel == IsRaised ) ;
if ( endOfRun )
   DoSomething;
Either of these two initializations will do, though the second with brackets is possibly clearer. If you use a flag to represent a condition that is true or false, use a bool variable.

On the next page : Nullable Types

Explore C / C++ / C#
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C# / C Sharp
  5. Learn C Sharp
  6. Using Numeric Suffixes

©2009 About.com, a part of The New York Times Company.

All rights reserved.