Why is C# statically typed?

前端 未结 17 2351
不思量自难忘°
不思量自难忘° 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:06

    Why C# requires me to specify the data type when creating a variable.

    Why do we need to know the data type before a class instance?

    I think one thing that most answers haven't referenced is the fact that C# was originally meant and designed as "managed", "safe" language among other things and a lot of those goals are arrived at via static / compile time verifiability. Knowing the variable datatype explicitly makes this problem MUCH easier to solve. Meaning that one can make several automated assessments (C# compiler, not JIT) about possible errors / undesirable behavior without ever allowing execution.

    That verifiability as a side effect also gives you better readability, dev tools, stability etc. because if an automated algorithm can understand better what the code will do when it actually runs, so can you :)

    0 讨论(0)
  • 2020-12-01 06:07

    You need [class name] in front because there are many situations in which the first [class name] is different from the second, like:

     IMyCoolInterface obj = new MyInterfaceImplementer();
     MyBaseType obj2 = new MySubTypeOfBaseType();
    

    etc. You can also use the word 'var' if you don't want to specify the type explicitely.

    0 讨论(0)
  • 2020-12-01 06:07

    c# is a strongly-typed language, like c++ or java. Therefore it needs to know the type of the variable. you can fudge it a bit in c# 3.0 via the var keyword. That lets the compiler infer the type.

    0 讨论(0)
  • 2020-12-01 06:07

    Statically typed means that Compiler can perform some sort of checks at Compile time not at run time. Every variable is of particular or strong type in Static type. C# is strongly definitely strongly typed.

    0 讨论(0)
  • 2020-12-01 06:08

    Because C# is a strongly typed language

    0 讨论(0)
提交回复
热议问题