Why does integer division in C# return an integer and not a float?

前端 未结 8 907
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 04:32

Does anyone know why integer division in C# returns an integer and not a float? What is the idea behind it? (Is it only a legacy of C/C++?)

In C#:

fl         


        
8条回答
  •  长发绾君心
    2020-11-21 05:24

    As a little trick to know what you are obtaining you can use var, so the compiler will tell you the type to expect:

    int a = 1;
    int b = 2;
    var result = a/b;
    

    your compiler will tell you that result would be of type int here.

提交回复
热议问题