Home > Article > Web Front-end > What is the difference between c# and JavaScript
Difference: 1. The var keyword in JavaScript can refer to different types, but not in C#; 2. JS has 6 basic data types and 1 reference data type, while C# has 16 predefined types and user-defined types; 3. String is a value (basic) type in JS, but is a reference (complex) data in C#.
The operating environment of this tutorial: windows7 system, javascript1.8.5&&C# version 8, Dell G3 computer. The difference between
1. In the var keyword
// C#中 var total = 15; var mec = new MyExcellentClass(); // 等价于 int total = 15; MyExcellentClass mec = new MyExcellentClass();
C#, The var keyword is not a symbol for a specific type of variable. It's just syntactic shorthand for any type that can be inferred from the right-hand side of an initialization statement. It is not like the var of js that can refer to different types. The var keyword does not change the strong type nature of C#.
2. Local variables in nested blocks
// C# void Method { var num1 = 5; { var num2 = 10; ... } ... }
// JS function Method1() { var num1 = 5; { var num2 = 10; ... } ... } // 采用es6的let变量 function Method2() { let num1 = 5; { let num2 = 10; ... } ... }
First of all, js did not have the concept of block-level scope before es6. That is to say, in function Method1, both num1 and num2 are in the same scope (that is, the function scope of Method1).
The let variable in es6 enables js to achieve the effect of block scope, that is, the variable num2 only exists in the adjacent pair of braces { }. At this time, the effect of the variable is the same as that of C#'s nested block .
Secondly, there is another difference. In JS, C and C, variables can be renamed. In the internal scope (block scope), the internal name masks the external name, and the value of the variable with the same name is renamed. Set. However, variables with the same name are not allowed in C# (regardless of nesting level, you cannot declare another local variable with the same name within the valid scope of the first name).
Finally, let me expand on this. Unlike JS, C, and C, there are no global variables or global functions in C#. Variables and functions must be declared inside the type (everything is in a class).
3. Data types
JS data types: 6 basic data types and 1 reference data type.
Basic data types | Number, String, Boolean, Undefined, Null, Symbol(es6) |
Reference (complex) data type | Object (including functions, arrays, regular expressions, and other types other than basic data types) |
C# data types: 16 predefined types and user-defined types.
11 numerical types |
Integer types: sbyte, byte, short, ushort, int, uint, long, ulong Floating point types: decimal, float, double |
1 Unicode character type | char |
bool | |
string (Unicode character array), |
object (base class for all types), dynamic (used when using an assembly written in a dynamic language) |
class | |
struct | |
array | |
enum | |
delegate | |
interface |
The above is the detailed content of What is the difference between c# and JavaScript. For more information, please follow other related articles on the PHP Chinese website!