Why is C# statically typed?

前端 未结 17 2848
不思量自难忘°
不思量自难忘° 2020-12-01 05:07

I am a PHP web programmer who is trying to learn C#.

I would like to know why C# requires me to specify the data type when creating a variable.

Class         


        
17条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 06:01

    When we define variables to hold data we have to specify the type of data that those variables will hold. The compiler then checks that what we are doing with the data makes sense to it, i.e. follows the rules. We can't for example store text in a number - the compiler will not allow it.

    int a = "fred"; // Not allowed. Cannot implicitly convert 'string' to 'int' 
    

    The variable a is of type int, and assigning it the value "fred" which is a text string breaks the rules- the compiler is unable to do any kind of conversion of this string.

提交回复
热议问题