Differences in syntax of C # and JavaScript

1. var keyword

// C#中
var total = 15;
var mec = new MyExcellentClass();

// 等价于
int total = 15;
MyExcellentClass mec = new MyExcellentClass();

In C #, var keyword is not a symbol of a particular type of variable. It's just shorthand syntactic , it means any type can be inferred from the right side of the initialization statement. Unlike the var js that can reference different types, does not change strongly typed keyword var properties of C #.

 

2. The local variables in a nested block

// 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, js is no block-level scope of this concept before es6, that is to say in the function Method1 num1 and num2 are in a same scope (i.e., the function scope Method1).

es6 let the variables that can be achieved js block scope effects, i.e., there is only the variable num2 adjacent pair of braces {}, in which case the same effect of variable C # nested blocks.

Secondly, there is a difference in the JS and C and C ++, the variables can be the same name inside the range (block scope), the internal name of the external cover name, the value of the variable with the same name is reset. However, C # variables are not allowed in the same name (regardless of how the nesting level, can not declare another local variable with the same name within the effective range of the first name).

Finally, the expansion explain, and JS, C, different C ++, C # there is no global variables, global functions, variables, and functions must be declared inside the type of (all things are in the class).

 

3. Data Types

JS data types: six kinds of basic data types, and one kind of reference data types.

JS data types
Basic data types Number,String,Boolean,Undefined,Null,Symbol(es6)
Reference (complex) data types Object (including all types of data in addition to the basic functions, arrays, regular expressions of that type)

C # data types: 16 predefined types and user-defined types.

C # 16 predefined types
11 kinds of numeric type

Integer type: sbyte, byte, short, ushort, int, uint, long, ulong

Floating-point type: decimal, float, double

One kind of Unicode character type char
One kind of boolean bool
Three kinds of complex types

string (Unicode character array),

object (the base class of all types),

(When using the dynamic assembly using the written language) dynamic

C # user-defined types
Class Type class
structure type struct
Array type array
Enumerated type enum
Delegate type delegate
Interface Type interface

A key distinction: string belongs JS value (base) type; string belonging to the reference (complex) data types in C #, its initial value is null, instead of "."

// C#中
string a; // 此处a的初始值为null,而不是""

Extension: C #, if not initialized variable, its value will be set to default values ​​the compiler, the default value is determined by the type field. Value type, the default value is 0; boolean types, the default is false; reference type, the default value is null. JS, if the direct use var declared variable, the default value is always undefined.

// JS中
var a;
console.log(a); // undefined

Key Difference between the two: the JS, C, different C ++, in C # does not have a sense of Boolean numbers .

// C#中
int x = 5;
if( x ) //错,x是int类型,不是布尔类型
  ...
if( x == 5 ) //对,因为表达式返回了一个布尔类型的值
  ...

 

References: "C # graphic tutorial (4th Edition)"

Published 175 original articles · won praise 345 · views 740 000 +

Guess you like

Origin blog.csdn.net/fifteen718/article/details/97935430