Rediger

Del via


Roadmap for JavaScript and TypeScript developers learning C#

C#, TypeScript and JavaScript are all members of the C family of languages. The similarities between the languages help you quickly become productive in C#.

  1. Similar syntax: JavaScript, TypeScript, and C# are in the C family of languages. That similarity means you can already read and understand C#. There are some differences, but most of the syntax is the same as JavaScript, and C. The curly braces and semicolons are familiar. The control statements like if, else, switch are the same. The looping statements of for, while, and do...while are same. The same keywords for class and interface are in both C# and TypeScript. The access modifiers in TypeScript and C#, from public to private, are the same.
  2. The => token: All languages support lightweight function definitions. In C#, they're referred to as lambda expressions, in JavaScript, they're typically called arrow functions.
  3. Function hierarchies: All three languages support local functions, which are functions defined in other functions.
  4. Async / Await: All three languages share the same async and await keywords for asynchronous programming.
  5. Garbage collection: All three languages rely on a garbage collector for automatic memory management.
  6. Event model: C#'s event syntax is similar to JavaScript's model for document object model (DOM) events.
  7. Package manager: NuGet is the most common package manager for C# and .NET, similar to npm for JavaScript applications. C# libraries are delivered in assemblies.

As you continue learning C#, you'll learn concepts that aren't part of JavaScript. Some of these concepts might be familiar to you if you use TypeScript:

  1. C# Type System: C# is a strongly typed language. Every variable has a type, and that type can't change. You define class or struct types. You can define interface definitions that define behavior implemented by other types. TypeScript includes many of these concepts, but because TypeScript is built on JavaScript, the type system isn't as strict.
  2. Pattern matching: Pattern matching enables concise conditional statements and expressions based on the shape of complex data structures. The is expression checks if a variable "is" some pattern. The pattern-based switch expression provides a rich syntax to inspect a variable and make decisions based on its characteristics.
  3. String interpolation and raw string literals: String interpolation enables you to insert evaluated expressions in a string, rather than using positional identifiers. Raw string literals provide a way to minimize escape sequences in text.
  4. Nullable and non-nullable types: C# supports nullable value types, and nullable reference types by appending the ? suffix to a type. For nullable types, the compiler warns you if you don't check for null before dereferencing the expression. For non-nullable types, the compiler warns you if you might be assigning a null value to that variable. These features can minimize your application throwing a System.NullReferenceException. The syntax might be familiar from TypeScript's use of ? for optional properties.
  5. LINQ: Language integrated query (LINQ) provides a common syntax to query and transform data, regardless of its storage.

As you learn more other differences become apparent, but many of those differences are smaller in scope.

Some familiar features and idioms from JavaScript and TypeScript aren't available in C#:

  1. dynamic types: C# uses static typing. A variable declaration includes the type, and that type can't change. There is a dynamic type in C# that provides runtime binding.
  2. Prototypal inheritance: C# inheritance is part of the type declaration. A C# class declaration states any base class. In JavaScript, you can set the __proto__ property to set the base type on any instance.
  3. Interpreted language: C# code must be compiled before you run it. JavaScript code can be run directly in the browser.

In addition, a few more TypeScript features aren't available in C#:

  1. Union types: C# doesn't support union types. However, design proposals are in progress.
  2. Decorators: C# doesn't have decorators. Some common decorators, such as @sealed are reserved keywords in C#. Other common decorators might have corresponding Attributes. For other decorators, you can create your own attributes.
  3. More forgiving syntax: The C# compiler parses code more strictly than JavaScript requires.

If you're building a web application, you should consider using Blazor to build your application. Blazor is a full-stack web framework built for .NET and C#. Blazor components can run on the server, as .NET assemblies, or on the client using WebAssembly. Blazor supports interop with your favorite JavaScript or TypeScript libraries.