Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0025 Default literal

The literal for real number is default to double. Therefore you have to add F suffix when initializing a float type variable. Otherwise the C# compiler will generate error message. using System; class Program { static void Main(string[] args) { float f = 1.1; Console.WriteLine(f); } } The code above generates the following error message: C:\g>csc Program.cs Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1 Copyright (C) Microsoft Corporation. All rights reserved. Program.cs(7,19): error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type The same rule applies to decimal type literal.