当前位置: 代码网 > it编程>编程语言>Asp.net > 浅谈熟悉C#如何转TypeScript

浅谈熟悉C#如何转TypeScript

2026年05月10日 Asp.net 我要评论
✅相同点(你已掌握的优势)特性说明静态类型ts 和 c# 都支持变量、参数、返回值的类型标注(如 string, number ≈ string, int)。类与面向对象class、int

✅相同点(你已掌握的优势)

特性说明
静态类型ts 和 c# 都支持变量、参数、返回值的类型标注(如 string, number ≈ string, int)。
类与面向对象class、interface、extends(继承)、implements(实现接口)语法高度相似。
泛型都用 <t>,如 list<t>(c#) ↔ array<t> 或 t[](ts)。
访问修饰符public/private/protected 在 ts 中用于编译期检查(运行时无作用,但 ide 支持好)。
async/await异步编程模型几乎一样:c# 用 task<t>,ts 用 promise<t>。

⚠️关键差异(需注意)

方面c#typescript
运行环境编译为 .net il,在 clr 上运行编译为 javascript,在浏览器或 node.js 中运行
类型系统强制、严格(编译失败即错误)可选 + 渐进式(可写纯 js,但推荐全类型)
基础类型int, bool, datetime 等基于 js:number(无 int/float 区分)、boolean、string;无内置日期类型(用 date 对象)
空值处理null + 可空类型 int?null 和 undefined 并存;开启 strictnullchecks 后类似 c# 可空引用
模块化namespace / 程序集es 模块:import { x } from './file' / export(无 namespace)
变量声明int x = 1; 或 var x = 1;必须用 let / const:const x: number = 1;
函数定义int add(int a, int b) => a + b;const add = (a: number, b: number): number => a + b;

🚀转型建议(3 步上手)

  1. 写法习惯调整

    • const/let 代替 var
    • import/export 组织代码
    • 函数多用箭头函数 () => {}
  2. 善用 ts 的“超能力”

    • 接口 (interface) 定义对象形状(比 c# 更灵活)
    • 联合类型:string | number
    • 类型推断强大,很多时候不用显式写类型
  3. 记住:ts ≠ 新语言,而是“带类型的 js”

    • 所有 js 语法都合法
    • 你的 c# 架构思维(solid、分层等)完全适用
    • 差别主要在标准库和运行时 api(比如发 http 请求用 fetch 而不是 httpclient

💡 一句话总结:

typescript = c# 的类型系统 + javascript 的灵活性 + 浏览器/node.js 运行时。

你已具备 80% 的核心能力,只需适应 js 生态和少量语法差异,就能高效开发!

c# 与 typescript 的典型代码对比

1.变量声明

// c#
string name = "alice";
int age = 30;
var isactive = true; // 类型推断
// typescript
const name: string = "alice";
let age: number = 30;
const isactive = true; // 类型自动推断为 boolean

✅ 建议:ts 中优先用 const(不可变)和 let(可变),避免 var

2.函数定义

// c#
public int add(int a, int b)
{
    return a + b;
}

// 或表达式体
public int multiply(int a, int b) => a * b;
// typescript
function add(a: number, b: number): number {
    return a + b;
}
// 或箭头函数(更常见)
const multiply = (a: number, b: number): number => a * b;
// 返回类型通常可省略(ts 能推断)
const subtract = (a: number, b: number) => a - b;

3.类与继承

// c#
public class animal
{
    public string name { get; set; }
    
    public animal(string name) => name = name;
    
    public virtual void speak() => console.writeline("...");
}

public class dog : animal
{
    public dog(string name) : base(name) { }
    
    public override void speak() => console.writeline("woof!");
}
// typescript
class animal {
    constructor(public name: string) {} // 自动创建并赋值 this.name
    speak(): void {
        console.log("...");
    }
}
class dog extends animal {
    speak(): void {
        console.log("woof!");
    }
}

✅ ts 的 constructor(public name: string) 是语法糖,等价于 c# 的自动属性初始化。

4.接口(interface)

// c#
public interface iperson
{
    string name { get; set; }
    int age { get; }
    void greet();
}
// typescript
interface person {
    name: string;
    readonly age: number; // 只读 ≈ c# 的 { get; }
    greet(): void;
}

💡 ts 接口用于描述对象形状,不限于类实现,也可用于普通对象:

const person: person = { name: "bob", age: 25, greet() { console.log("hi"); } };

5.泛型(generics)

// c#
public class box<t>
{
    public t value { get; set; }
    
    public box(t value) => value = value;
}

var numberbox = new box<int>(42);
// typescript
class box<t> {
    constructor(public value: t) {}
}
const numberbox = new box<number>(42);
// 或让 ts 推断
const stringbox = new box("hello");

6.异步编程

// c#
public async task<string> fetchdataasync()
{
    using var client = new httpclient();
    return await client.getstringasync("https://api.example.com/data");
}
// typescript(浏览器或 node.js 环境)
async function fetchdata(): promise<string> {
    const response = await fetch("https://api.example.com/data");
    return await response.text();
}
// 或使用 axios(需安装)
import axios from 'axios';
async function fetchdata2(): promise<string> {
    const res = await axios.get<string>("https://api.example.com/data");
    return res.data;
}

✅ 概念一致:async/await + 容器类型(task<t> ↔ promise<t>)

7.空值处理(启用 strict 模式)

// c#
string? maybename = getname(); // 可空引用类型(c# 8+)
if (maybename != null)
    console.writeline(maybename.length);
// typescript(tsconfig.json 中开启 "strictnullchecks": true)
function getname(): string | null {
    return math.random() > 0.5 ? "alice" : null;
}
const maybename = getname();
if (maybename !== null) {
    console.log(maybename.length); // 安全访问
}

🔒 强烈建议在 tsconfig.json 中启用 strict: true,获得接近 c# 的空安全体验。

总结:你的 c# 经验可以直接迁移!

c# 概念typescript 对应
classclass(几乎一样)
interfaceinterface(更灵活)
list<t>t[] 或 array<t>
task<t>promise<t>
namespace不用 → 改用 import/export
httpclientfetch 或 axios

你缺的只是 javascript 运行时 api前端/node.js 生态 的熟悉度,语言本身对你来说几乎没有学习曲线!

到此这篇关于浅谈熟悉c#如何转typescript的文章就介绍到这了,更多相关c#转typescript内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com