C# Error – Literal of type double cannot be implicitly converted to type ‘float’; use an ‘F’ suffix to create a literal of this type

There are times when you get the below error when building a C# application.

C# Error – Literal of type double cannot be implicitly converted to type ‘float’; use an ‘F’ suffix to create a literal of this type

image

You would get the above error when trying to execute this float floatVal = 10.5; from the below code snippet.

using System;
using System.Collections.Generic;

namespace DeveloperPublishCodeConsoleApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            float floatVal = 10.5;
            Console.ReadLine();
        }
    }
}

In the above code snippet , the error was caused because the value 10.5 is by default inferred to be of type double . This does not have implicit conversion to float .

Using the suffix “F” for the value will fix this issue as shown below.

float floatVal = 10.5F;
Console.ReadLine();

Leave a Reply