Table of Contents
c# and JavaScript
Home Web Front-end Front-end Q&A What is the difference between c# and JavaScript

What is the difference between c# and JavaScript

Jan 12, 2022 pm 04:23 PM
c# 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#.

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();
Copy after login

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;
    ...
  }
  ...
}
Copy after login
// JS
function Method1()
{
  var num1 = 5;
  {
    var num2 = 10;
    ...
  }
  ...
}
 
// 采用es6的let变量
function Method2()
{
  let num1 = 5;
  {
    let num2 = 10;
    ...
  }
  ...
}
Copy after login

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 typesNumber, String, Boolean, Undefined, Null, Symbol(es6)
Reference (complex) data typeObject (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,而不是""
Copy after login

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

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

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

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

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

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

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

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")
}
Copy after login

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")
})
Copy after login

控制台输出如下:

【相关推荐:javascript学习教程

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

See all articles
class
struct
array
enum
delegate
interface