Home  >  Article  >  Web Front-end  >  What is the difference between c# and JavaScript

What is the difference between c# and JavaScript

青灯夜游
青灯夜游Original
2022-01-12 16:23:204521browse

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#.

What is the difference between c# and JavaScript

The operating environment of this tutorial: windows7 system, javascript1.8.5&&C# version 8, Dell G3 computer. The difference between

c# and JavaScript

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.

JS data types
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.

##1 Boolean typebool##3 complex typesstring (Unicode character array),
C#16 predefined 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

object (base class for all types),

dynamic (used when using an assembly written in a dynamic language)

C#User-defined typeClass typeStructural type Array typeEnumeration typeDelegate typeInterface type

重点区别一:string在JS中属于值(基本)类型;string在C#属于引用(复杂)数据类型,其初始值为null,而不是""。

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

延伸:C#中,如果没有初始化的变量,其值会被编译器设为默认值,默认值由字段的类型决定。值类型,默认值为0;布尔类型,默认值为false;引用类型,默认值为null。JS中,如果是直接用var声明的变量,其默认值一律为undefined。

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

重点区别二:与JS、C、C++不同,在C#中数字不具有布尔意义

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

重点区别三:单引号与双引号意义不同。

// C#中,假设'A'为char类型,如果把字符放在双引号中,编译器会把它看成字符串,从而产生错误。

// JS中,单引号和双引号一样,都用于表示字符串(string类型):
'A' === "A" // true

4. foreach 和 forEach

两者在使用过程中的最大区别就是C#中可以通过break语法来跳出循环,而在JS中forEach正常来说是没得法子跳出循环的

注意:两者大小写不同,另外C#中foreach可用于数组、字符串或集合类数据类型。JS中forEach仅用于数组对象。

代码参照如下:

// C#中
int[] a = {1, 2, 3, 4, 5};
foreach (var item in a) {
  Console.WriteLine(item + "-- start")
  if(item > 3) break;
  Console.WriteLine(item + "-- end")
}

JS代码通过控制台的测试截图可以更加直观的理解,测试代码如下:

// JS测试代码,可用于控制台输出比较
var a = [1, 2, 3, 4, 5];

// break --语法报错
a.forEach(item => {
  console.log(item + "-- start")
  if(item > 3) break;
  console.log(item + "-- over")
})

// return --并非跳出循环
a.forEach(item => {
  console.log(item + "-- start")
  if(item > 3) return;
  console.log(item + "-- over")
})

// throw new Error --非主流行为,的确可以跳出循环
a.forEach(item => {
  console.log(item + "-- start")
  if(item > 3) throw new Error("-- 强行报错来终止遍历");
  console.log(item + "-- over")
})

控制台输出如下:

【相关推荐:javascript学习教程

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn