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#.
- 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 offor
,while
, anddo
...while
are same. The same keywords forclass
andinterface
are in both C# and TypeScript. The access modifiers in TypeScript and C#, frompublic
toprivate
, are the same. - 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. - Function hierarchies: All three languages support local functions, which are functions defined in other functions.
- Async / Await: All three languages share the same
async
andawait
keywords for asynchronous programming. - Garbage collection: All three languages rely on a garbage collector for automatic memory management.
- Event model: C#'s
event
syntax is similar to JavaScript's model for document object model (DOM) events. - 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:
- C# Type System: C# is a strongly typed language. Every variable has a type, and that type can't change. You define
class
orstruct
types. You can defineinterface
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. - 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-basedswitch
expression provides a rich syntax to inspect a variable and make decisions based on its characteristics. - 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.
- 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 fornull
before dereferencing the expression. For non-nullable types, the compiler warns you if you might be assigning anull
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. - 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#:
- 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. - 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. - 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#:
- Union types: C# doesn't support union types. However, design proposals are in progress.
- 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. - 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.