C#: Casting '0' to int

后端 未结 3 1953
Happy的楠姐
Happy的楠姐 2020-12-20 13:29

I saw a code like this:

private readonly object[] m_Values = { (int)0, (int)0 };

What\'s the idea to cast 0 to int? Isn\'t it int by \'defa

3条回答
  •  萌比男神i
    2020-12-20 14:07

    C# defaults to Int32 when you declare an integer literal without supplying a type hint (as long as the literal fits into an Int32, otherwise it will go to Int64). From here:

    In C#, literal values receive a type from the compiler. You can specify how a numeric literal should be typed by appending a letter to the end of the number. For example, to specify that the value 4.56 should be treated as a float, append an "f" or "F" after the number

提交回复
热议问题