C# 是 TypeScript 的最佳替补?

2021/12/28 11:08:19

本文主要是介绍C# 是 TypeScript 的最佳替补?,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

C# 是 TypeScript 的最佳替补?

TypeScript非常优秀。它完美地结合了强类型和快速开发,因此非常好用,我在许多情况下都会默认选择这个库。但是,世上没有完美的语言,有些情况下TypeScript并不是最合适的工具:

  • 性能至关重要(例如实时通信、视频游戏)
  • 需要与原生代码(如C/C++或Rust)交互
  • 需要更严格的类型系统(例如金融系统)

对于这些情况,TypeScript开发人员最好还是选用其他语言。C#、Go和Java都是非常好的选择。它们的速度远超 TypeScript,每种语言都有自己的长处。C#能与TypeScript配合得很好,我来解释一下为什么。

 

 

 

TypeScript 就是添加了 C# 的 JavaScript

C#能与TypeScript配合得很好,因为它们看上去就像是同一种语言。两者都是由Anders Hejlsberg设计的,而且从许多方面来看,TypeScript就是添加了C#的JavaScript。它们的特性和语法都很相似,因此在同一个项目中结合使用二者非常容易。更重要的是,C#的语言与TypeScript很相似,因此开发人员阅读和编写代码也非常轻松。相反,Go是一种完全不同的语言:没有类,没有继承,没有异常,没有包级别的封装(只有类级别的封装),而且语法也完全不同。当然这并不一定是坏事,但开发人员的确需要重新思考并用不同的方式设计代码,因此,同时使用Go和TypeScript是比较困难的。不过,Java与C#很相似,但依然缺乏许多C#和TypeScript都有的功能。

 

C#和TypeScript的相似之处

也许你已经知道,C#和TypeScript有很多相似之处,如基于C的语法、类、接口、泛型等。下面,我来详细列举一下二者的相似之处:

  • async/await
  • lambda表达式和函数式数组方法
  • 用于处理空的操作符(?,!,??)
  • 解构
  • 命令行界面(CLI)

async/await

首先,C#和JavaScript都使用async/await来处理异步代码。在JavaScript中,异步操作用Promise表示,而应用程序可以await一个异步操作结束。C#中的Promise其实是Task,概念上与Promise完全相同,也有相应的方法。下面的例子演示了两种语言中async/await的用法:

1 async function fetchAndWriteToFile(url: string, filePath:string): Promise<string> {
2   // fetch() returns aPromise
3   const response = awaitfetch(url);
4   const text = awaitresponse.text();
5   // By the way, we'reusing Deno (https://deno.land)
6   awaitDeno.writeTextFile(filePath, text);
7   return text;
8 }

 

 TypeScript中async/await的例子

 1 using System.IO;
 2 using System.Net.Http;
 3 using System.Threading.Tasks;
 4 
 5 async Task<string> FetchAndWriteToFile(string url, stringfilePath) {
 6   // HttpClient.GetAsync()returns a Task
 7   var response = await newHttpClient().GetAsync(url);
 8   var text = awaitresponse.Content.ReadAsStringAsync();
 9   awaitFile.WriteAllTextAsync(filePath, text);
10   return text;
11 }

 

 C#中async/await的例子

下面是JavaScript的Promise API与等价的C# Task API:

JavaScript API

等价的C# API

Promise.all()

Task.WaitAll()

Promise.resolve()

Task.FromResult()

Promise.reject()

Task.FromException()

Promise.prototype.then()

Task.ContinueWith()

new Promise()

new TaskCompletionSource()

Lambda表达式和函数式数组方法

C#和JavaScript都用熟悉的=>语法(即箭头函数)来表示lambda表达式。下面是TypeScript和C#的比较:

1 const months = ['January', 'February', 'March', 'April'];
2 const shortMonthNames = months.filter(month => month.length< 6);
3 const monthAbbreviations = months.map(month =>month.substr(0, 3));
4 const monthStartingWithF = months.find(month => {
5   returnmonth.startsWith('F');
6 });

 

TypeScript中使用lambda表达式

1 using System.Collections.Generic;
2 using System.Linq;
3 
4 var months = new List<string> {"January","February", "March", "April"};
5 var shortMonthNames = months.Where(month => month.Length <6);
6 var monthAbbreviations = months.Select(month =>month.Substring(0, 3));
7 var monthStartingWithF = months.Find(month => {
8   returnmonth.StartsWith("F");
9 });

 

 C#中使用lambda表达式

上述示例演示了C#的System.Linq命名空间中的一些方法,相当于JavaScript的函数式数组方法。下面是JavaScript的数组方法与等价的C# Linq方法:

JavaScript API

等价的C# API

Array.prototype.filter()

Enumerable.Where()

Array.prototype.map()

Enumerable.Select()

Array.prototype.reduce()

Enumerable.Aggregate()

Array.prototype.every()

Enumerable.All()

Array.prototype.find()

List.Find()

Array.prototype.findIndex()

List.FindIndex()

处理空操作符

C#和TypeScript处理空的特性也一样:

Feature nameSyntaxDocumentation links
Optional properties property? TS :https://www.typescriptlang.org/docs/handbook/2/objects.html#optional-properties

C#:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-reference-types

Non-null assertion object!.property

TS:https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator

C#:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving

Optional chaining object?.property

JS :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

C#:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-

Nullish coalescing object ?? alternativeValue JShttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator

C#:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator

解构

尽管C#默认不支持数组或类的解构,但它支持Tuple和Record的解构,用户也可以为自定义类型定义解构。下面是TypeScript和C#中解构的例子:

const author = { firstName: 'Kurt', lastName: 'Vonnegut' };
// Destructuring an object:
const { firstName, lastName } = author;

const cityAndCountry = ['Indianapolis', 'United States'];
// Destructuring an array:
const [city, country] = cityAndCountry;

 

 TypeScript中解构的例子

 1 using System;
 2 
 3 var author = new Author("Kurt", "Vonnegut");
 4 // Deconstructing a record:
 5 var (firstName, lastName) = author;
 6 
 7 var cityAndCountry = Tuple.Create("Indianapolis","United States");
 8 // Deconstructing a tuple:
 9 var (city, country) = cityAndCountry;
10 
11 // Define the Author record used above
12 record Author(string FirstName, string LastName);

 

 C#中解构的例子

命令行界面(CLI)

我的开发方式是使用文本编辑器编写代码,然后在终端运行命令,构建并运行。对于TypeScript,这意味着需要使用node或deno命令行界面(CLI)。C#也有类似的CLI,名为dotnet(由C#的.NET运行时得名)。下面是使用dotnet CLI的一些例子:

 1 mkdir app && cd app
 2 # Create a new console application
 3 # List of available app templates:https://docs.microsoft.com/dotnet/core/tools/dotnet-new
 4 dotnet new console
 5 # Run the app
 6 dotnet run
 7 # Run tests (don't feel bad if you haven't written those)
 8 dotnet test
 9 # Build the app as a self-contained
10 # single file application for Linux.
11 dotnet publish -c Release -r linux-x64

 

 

基本功能(类、泛型、错误和枚举)

这些是TypeScript和C#之间更基本的相似性。下面的例子是有关这几个方面的介绍:

 1 import { v4 as uuidv4 } from'https://deno.land/std/uuid/mod.ts';
 2 
 3 enum AccountType {
 4   Trial,
 5   Basic,
 6   Pro
 7 }
 8 
 9 interface Account {
10   id: string;
11   type: AccountType;
12   name: string;
13 }
14 
15 interface Database<T> {
16   insert(item: T):Promise;
17   get(id: string):Promise<T>;
18 }
19 
20 class AccountManager {
21 
22   constructor(database:Database<Account>) {
23     this._database =database;
24   }
25 
26   asynccreateAccount(type: AccountType, name: string) {
27     try {
28       const account = {
29         id: uuidv4(),
30         type,
31         name;
32       };
33       awaitthis._database.insert(account);
34     } catch (error) {
35         console.error(`Anunexpected error occurred while creating an account. Name: ${name}, Error:${error}`);
36     }
37   }
38 
39   private _database:Database<Account>;
40 }

 

 TypeScript类的示例

 1 using System;
 2 using System.Threading.Tasks;
 3 
 4 enum AccountType {
 5   Trial,
 6   Basic,
 7   Pro
 8 }
 9 
10 record Account(string Id, AccountType Type, string Name);
11 
12 interface IDatabase<T> {
13   Task Insert(T item);
14   Task<T> Get(stringid);
15 }
16 
17 class AccountManager {
18 
19   publicAccountManager(IDatabase<Account> database) {
20     _database = database;
21   }
22 
23   public async voidCreateAccount(AccountType type, string name) {
24     try {
25       var account = newAccount(
26        Guid.NewGuid().ToString(),
27         type,
28         name
29       );
30       await_database.Insert(account)
31     } catch (Exceptionexception) {
32      Console.WriteLine($"An unexpected error occurred while creating anaccount. Name: {name}, Exception: {exception}");
33     }
34   }
35 
36   IDatabase<Account>_database;
37 }

 

 C#类的示例

C#的其他优势

与TypeScript相似并不是C#的唯一优点,它还有其他优点:

  • 与原生代码结合更容易
  • 事件
  • 其他功能

与原生代码结合

C#的最大优势之一就是它可以深入原生代码。本文开头提到,TypeScript并不擅长与C/C++代码结合。Node.js有一个支持原生C/C++的插件,名为Node-API,但是它需要为原生函数编写额外的C++包裹器,将原生类型转换成JavaScript对象,或相反,类似于JNI的工作方式。而C#可以直接调用原生函数,只需把库放到应用程序的bin目录下,然后将API定义为C#中的外部函数即可。然后就能像C#函数一样使用外部函数,.NET运行时会处理好C#数据类型与原生数据类型之间的转换。例如,如果原生库导出了下面的C函数:

1 int countOccurrencesOfCharacter(char *string, char character) {
2     int count = 0;
3     for (int i = 0;string[i] != '\0'; i++) {
4         if (string[i] ==character) {
5             count++;
6         }
7     }
8     return count;
9 }

 

 那么可像下面这样从C#中调用:

 1 using System;
 2 using System.Runtime.InteropServices;
 3 
 4 var count = MyLib.countOccurrencesOfCharacter("C# is prettyneat, eh?", 'e');
 5 // Prints "3"
 6 Console.WriteLine(count);
 7 
 8 class MyLib {
 9     // Just placeMyLibraryName.so in the app's bin folder
10    [DllImport("MyLibraryName")]
11     public static externint countOccurrencesOfCharacter(string str, char character);
12 }

 

 这种方法可以通过C连接访问任何动态库(.so、.dll或.dylib),也就是说,你可以轻松地调用C、C++、Rust、Go或其他语言编写的代码,只要编译成机器码即可。原生交互的其他应用还有:

    • 将指针作为IntPtr传给原生对象
    • 利用GetFunctionPointerForDelegate()将C#方法作为函数指针传给原生函数
    • 使用Marshal.PtrToStringAnsi()将C字符串转换为C#字符串
    • 转换结构和数组

 

事件

C#的一个独特的特性是,提供了一流的事件支持。在TypeScript中,你可以实现addEventListener()方法,让客户端监听事件,而C#有event关键字,可以用来定义事件,并通过简单的语法将事件通知给所有监听者(而不需要像TypeScript那样手动遍历所有事件监听者并在try/catch块中执行)。例如,我们可以让Connection类定义一个MessageReceived事件,如下所示:

1 class Connection {
2     // AnAction<string> is a callback that accepts a string parameter.
3     public eventAction<string> MessageReceived;
4 }

 

 使用Connection代码可以通过+=操作符给MessageReceived添加一个处理函数,如下:

1 var connection = new Connection();
2 connection.MessageReceived += (message) => {
3    Console.WriteLine("Message was received: " + message);
4 };

 

 而Connection类可以在内部调用MessageReceived,为所有监听者触发MessageReceived事件:

1 // Raise the MessageReceived event
2 MessageReceived?.Invoke(message);

 

 

其他优势

  • 性能:C#很快。C#的ASP.NET Web框架一直在Techempower的评测中名列前茅,而C#的.NET CoreCLR运行时的性能每个主要版本都在提高。C#拥有优良性能的原因之一是,通过使用结构而不是类,应用程序可以最小化甚至完全消除垃圾回收。因此,C#在视频游戏编程中非常流行。
  • 游戏和混合现实:C#是游戏开发最流行的语言之一,像Unity、Godot甚至Unreal游戏引擎都使用了C#。C#在混合现实中也很流行,因为VR和AR应用程序都是用Unity编写的。
  • 由于C#拥有第一方库、工具和文档,因此一些任务非常容易实现,比如,在C#中创建gRPC客户端要比TypeScript方便得多。相反,在Node.js中使用TypeScript时,就必须找出正确的模块和工具的组合,才能正确地生成JavaScript gRPC客户端,以及相应的TypeScript类型。
  • 高级功能:C#有许多其他语言没有的功能,如运算符重载、析构函数等。

总结

如前所述,世上没有完美的语言。在设计语言时总要有所权衡,所以一些语言的速度更快,但使用难度会增加(例如Rust的借出检查)。另一方面,一些语言非常易用,但通常性能的优化难度就会增加(例如JavaScript的动态语言特性)。正因如此,我相信掌握一组相似的语言会非常有用:这些语言分别有各自的长处,但都很相似,而且能互相配合。例如,下面是我选择的一组语言:

TypeScript

  • 最高层的语言,开发速度最快

  • 性能并非最佳,但适用于大多数应用

  • 不太适合与原生代码结合

C#

  • 仍然是高级语言,支持垃圾回收,所以很容易使用,尽管并不如TypeScript那么容易。

  • 从速度和内存占用量来看,其性能都优于 TypeScript

  • 最重要的是,能够与底层很好地结合

C++

  • 开发难度较大(例如需要手动内存管理),因此开发速度会慢很多

  • 但运行时的性能最佳!而且随处可用,能与许多已有的软件相结合

  • 很像C#,而且标准库很好,但也有许多陷阱(大多数与内存管理有关)。我更希望使用Rust,因为它的内存安全性更好,但我的许多工作都要与已有的C++代码结合,因此使用C++会更容易。

 



这篇关于C# 是 TypeScript 的最佳替补?的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程