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

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...