Friday 26 January 2018

Why Switch not Use Float and Double Value in C#.

In this article, We will discuss about Why Switch not Use Float and Double Value in C#.

Switch not Use Float and Double Value because it's value is not fixed at a particular amount of time or we can say that it's value is not Precise.

But still if we Use Float and Double value in Switch then it Will Show you error and Which is Shown in below figure,


To Resolve the error We can Convert that float or double value into int and then the Switch Will Work Perfectly like this,

private void Test()
        {
            float f = 3.51f;

            switch ((int)(f * 100))
            {
                case 350:
                    break;
            }
        }

0 comments:

Post a Comment