Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why c# treats 0 as int and not as short/decimal etc?

Tags:

c#

In c# when i say :

var s = 0;

what should be type of s ? it makes it int32. then seems like var is not usable for types like short and others(?).

like image 512
Dhananjay Avatar asked Mar 15 '12 10:03

Dhananjay


People also ask

Why should you learn C?

Being a middle-level language, C reduces the gap between the low-level and high-level languages. It can be used for writing operating systems as well as doing application level programming. Helps to understand the fundamentals of Computer Theories.

Why C language is named so?

C is a general purpose computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. It was named 'C' because many of its features were derived from an earlier language called 'B'.

Is C the best language?

C may be best because it underpins most of the IT industry; without C, we wouldn't have Windows, macOS, Linux, Android, iOS, etc.; without C, we wouldn't have most of our language compilers. C may be best for efficient code generation. Very fast, compact code, ideal for embedded applications.

Why semicolon is used in C?

Semicolons are end statements in C. The Semicolon tells that the current statement has been terminated and other statements following are new statements. Usage of Semicolon in C will remove ambiguity and confusion while looking at the code.


3 Answers

From the specification (§ 2.4.4.2):

If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint, long, ulong.

So smaller types are not automatically inferred. You can add a cast to force such a smaller type:

var s = (short)0;
like image 27
Joey Avatar answered Sep 28 '22 11:09

Joey


Look at this post here at StackOverflow.

You can specify a postfix for all numbers.

var i = 0;
var d = 0d;
var f = 0f;
var l = 0L;
var m = 0m;
like image 88
Malmi Avatar answered Sep 28 '22 12:09

Malmi


As per the C# language standard, specifically §2.4.4.2 on integer literals:

The type of an integer literal is determined as follows:

  • If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint, long, ulong.
  • If the literal is suffixed by U or u, it has the first of these types in which its value can be represented: uint, ulong.
  • If the literal is suffixed by L or l, it has the first of these types in which its value can be represented: long, ulong.
  • If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it is of type ulong.

The first rule is all that needs to be applied here. The literal 0 has no suffix and can be represented as type int (or, in BCL terms, Int32), so that's the type it has.

As you can infer from the above, to change the type of a literal, you can append a suffix to it. For example, the literal 0u will be of type uint. Alternatively, you could explicitly cast the literal to a different type; for example: (short)0 would cause the compiler to treat the literal as a short.

like image 27
Cody Gray Avatar answered Sep 28 '22 11:09

Cody Gray