Double and Float Tip in C#

I wrote a code snippet that day, and Visual Studio throws an error message regarding type conversation between Double and Float.

This is the original code:

float number = 1.35;
//Compiler throw an error

As you can see, I declared a variable named number, and its data type is Float. The number’s value is 1.35. However, Visual Studio throws the error as:

Literal of type double cannot be implicit converted to type ‘float’; use an ‘F’ suffix to create a literal of this type

Why does it happen?

Double and Float

The documentation in Microsoft explains why. Have a look the chart blow.

The double type has a larger range than Float type in C#; its size is larger than Float type in memory. The compiler chooses the data type which has a larger size in memory by default.

Let’s go back to our code. The compiler thinks 1.35 is a Double type by default, which prevents from trigger a memory leak. To use the Float data type, we need to add a suffix f in our code. The new code should be:

float number = 1.35f;
//Compiler know this is a float type precisely.

4 Replies to “Double and Float Tip in C#”

  1. I precisely desired to say thanks again. I’m not certain the things I could possibly have carried out in the absence of the actual creative concepts discussed by you concerning my concern. It absolutely was a depressing concern for me personally, but considering a new professional avenue you handled it forced me to cry for delight. I’m grateful for the assistance and as well , expect you comprehend what a powerful job that you are carrying out educating men and women by way of a web site. I am certain you’ve never come across all of us.

  2. I simply wished to thank you very much yet again. I do not know what I would have undertaken without those tips shown by you relating to this theme. Previously it was the terrifying setting in my view, nevertheless understanding your well-written technique you treated the issue took me to weep with joy. Now i’m thankful for your support as well as trust you recognize what an amazing job you were undertaking training the others via your website. Most probably you haven’t met any of us.

  3. I simply needed to thank you so much once again. I am not sure the things that I would’ve implemented without the type of recommendations provided by you on such a question. It previously was a very frightful dilemma in my opinion, however , noticing the very specialised mode you processed that made me to leap over gladness. I am just happy for this information and in addition hope that you know what a great job you are always getting into teaching people thru your web blog. More than likely you have never got to know any of us.

Leave a Reply

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