Syntax Video
Getting started with C# for TypeScript Devs
- 0:00 Intro
- 0:30 Setup
- 0:53 Learning Plan
- 1:08 C Sharp Syntax First Look
- 1:31 var, arrays and foreach
- 2:07 Language-Integrated Query (LINQ)
- 2:32 Pattern Matching
- 3:09 Records
- 3:52 C# Learning Resources
- 4:36 ASP.NET Minimal API Example
- 6:22 Swagger / OpenAPI Documentation
- 7:04 TypeScript Client Code Generation
- 7:34 ASP.NET API / OpenAPI Example
- 9:21 OpenAPI TypeScript Client
- 10:12 .NET Versions
- 10:30 ASP.NET Razor, MVC, Blazor
- 11:08 What is Sentry?
- 11:36 Full Stack Error Tracing with Sentry
- 12:48 How to Add Sentry to your Apps
- 13:33 Thanks!
Intro
what's up CJ here in this video I'm going to talk about C specifically for typescript and JavaScript web developers and I'm going to talk about how you can get started in learning and Building Things with C now this video was inspired by episode 781 of syntax on that episode Scott and I were answering viewer questions and specifically one viewer asked uh what language should they learn next to kind of Branch out from JavaScript and typescript and my answer for this was C sharp so let me tell you what you can do I'm going to link this document down in the description it has a ton of links to where you can get started with all the stuff that I'm going to talk about but the first thing you'll need to do is set up your local environment so I'll link to that article there it tells you everything you need also you'll need to install the net runtime so you can click that link there and then there's also the C dev kit for vs code so that gives you everything you need inside of VSS code to be able to write C programs now I have a tiny little learning plan for you here and the first step in all of this is to familiarize yourself with the
Setup
Learning Plan
c l anguage there's a lot of things you can do with C but I would start with just learning the syntax and building out some basic programs and so let me show you some of the syntax right now uh you can see here if we want to Output to the console it's just console. rightline we can pass in a string strings in C use double quotes uh semicolons are required um but you will see this looks very similar to to uh typescript in JavaScript and that's because C is a c style language just like JavaScript and typescript are a c style language and so this is how we output to the console we can also create lists of strings you see here I have CJ West and Scott in this list here you also see that I have the VAR keyword uh C is not a dynamic language like JavaScript VAR is actually just a shorthand the type here still is a list of strings uh but we can use VAR so that way we don't have to type out the type name on the left hand side similarly here name is going to be a string but we can use the VAR keyw and they have string interpolation so this looks very similar to template strings
C Sharp Syntax First Look
var, arrays and foreach
in JavaScript although I would argue a little cleaner because you only have to have one sign uh but that's the basics you can create lists for each is buil in so you can iterate over them it's pretty slick uh one of the other cool things about C is this thing called language integrated query or link this is kind of like map filter reduce in JavaScript uh but you can operate on arrays and lists and you have Lambda functions so even though C is an object-oriented programming language you can still do functional like things which is pretty slick so here I'm grabbing all the names from that list whose length is greater than two sorting them and then spitting them out to the console another cool thing in C is pattern matching um and so I first came across pattern matching when I was learning rust and this is really cool because it's more powerful than just a regular switch statement because the thing you're matching on can be an actual expression and so this here is a Lambda function that's the other thing is we can Define Lambda functions in line and uh on the left hand side we have an expression that's going to be evaluated for the thing that we're
Language-Integrated Query (LINQ)
Pattern Matching
matching on and if it evaluates to True we'll run the the expression on the right hand side and so for instance we can invoke that function pass in 32 and we'll get back that specific State then output it to the console so pattern matching pretty slick and then another really cool thing about modern C is this idea of Records so if you're familiar with creating classes and objects like in Java and also in C which you can still do this is similar but much much less code and so the idea here is you can define a record of some type you define all of the properties that it needs to have and this is all the code you need so this is a whole lot less boiler plate than like creating a class and a Constructor and like fields and Getters and Setters and stuff like that this one line of code is all we need to create a class with two public properties first name and last name and then we can create instances of it we can output it to the console it's really slick now there's a lot more stuff in C I just wanted to show you some of the the cool modern features of C so you should definitely check it out now here in the resources I have various places where you can go learn about C and one
Records
C# Learning Resources
of the nicest things is pretty much everything you need to learn about C you can find in the official documentation so Microsoft has put a ton of work into making sure that the tutorials are good the documentation is good the resources are good so they even have entire free video series on learning C so definitely check out the C for beginners video series learn more about the language and it's a full series that's going to teach you everything you need to know about the basics now beyond that I would practice with c and one of the ways I like to do that is with Cod Wars so go on code Wars pick some of the easier problems and just try to solve them with C because from from there you're going to learn about some of the built-in methods and and ways of working with data types and stuff like that so I would start there from there you want to start building real applications and I would focus my efforts on asp.net minimal API let's take a look at it because if you have ever built an Express API this is going to look very similar to you essentially you can define a web application and then Define your endpoints so this says when a get
ASP.NET Minimal API Example
route comes into slash respond with the string hello world and then run the application um and so this is like I said this is going to be very familiar to you if you've ever written an Express app with typescript or with JavaScript and let's actually generate one really quick just to see how it works so in this directory I'm going to make a directory called um minimal API go in that directory and then if I say net new web this is going to create a super basic minimal web application we'll open it up and if you look at the codes it's exactly like that example we just saw but if I start it up I can say net run so that'll start it up on my machine and now I have a a working web API and I can add new inp points to it so for instance I could add a to-dos inpoint so we could say like map get if we go to SL todos this is just going to be a little Lambda function that returns a new array of strings so we'll have something like learn C learn.net boom I've got a
new endpoint now I will have to restart this but uh the CLI comes with a Watcher so if you say net watch run uh this will automatically reload anytime there are code changes and so now our API is up and running but if I go to SL toos it returns a list of to-dos you'll also notice that it automatically stringified it into Json so modern C and like the minimal API here is really slick and uh you're definitely going to like it if you're coming from like Express or different things like that now another cool thing built into the net ecosystem is Swagger and open API integration so now that I've defined this API probably want some kind of documentation that says what are the endpoints how do I call them and in the world of JavaScript and typescript there there are some packages that'll let you do this but it's a lot more manual but there are Integrations I can add that I literally just attach them to the Builder here and that will automatically generate documentation for this API it'll say what endpoints exist what data does it accept and so um I recommend check out
Swagger / OpenAPI Documentation
the minimal API overview and then they have this tutorial create a minimal API and they'll walk you through creating a full crud app for to-dos and they also add Swagger and open API integration from there you're going to probably want to talk to your API from a JavaScript or typescript web application and because it's a rest API you can do that with fetch but you probably want some kind of type safety and the nice thing about Swagger and open API is that it generates a specification for your API and there's a whole ecosystem of tools that can take that specification and generate a fully typed client that you can use in your front-end JavaScript and typescript we application so actually let me show you an example of this I'm going to pull it up really quick and I'll show you what that looks like so for this application I went through that minimal API tutorial and built out a to-do's API but you'll see that they add a bunch of other stuff that you typically will need for an API things like talking to a database and then I also went beyond that and found their tutorial on adding authentication and authorization you can see that we here have the Swagger generation and and open
TypeScript Client Code Generation
ASP.NET API / OpenAPI Example
API document generation we also integrate with Sentry which I'll talk about in a second and so basically I'm adding all the things that you would need for like a real production level API and uh from there you can see our actual endpoints so if I look in routes I have a file called to do item routes and then I have a function for all of our crud routes right so get all items delete an item insert an item and you'll see what we're looking at right now is just just C code I don't have any extra stuff that tells it like how to document this stuff these are literally just endpoints and then I map these endpoints to specific URLs but that's all that's needed to generate an entire spec for this API right I don't have to do anything extra I just have all of the code that defines the endpoints and then in my setup of the app here I've enabled open API and Swagger but from there you automatically get this Swagger UI and because I added that identity you can see that I have endpoints for registration and login and email confirmation and password reset all of those endpoints automatically got added just by adding this uh identity
integration and then from there you can see that it documents all of my to-do items in points and it tells me the type that I'm going to get back here and when I'm making a post request and I try it out it gives me an example type here because it was able to infer that from all of the types and it was able to generate uh this entire spec in documentation and so so if you're new to this this actually is based on the open API spec you can see the the spec that gets generated here uh but this is actually what's being used by Swagger UI to be able to to know what all my endpoints are but one of the cool things is once you have an API specification like this you can generate a typed client and so let me show you that I have here a react application and I used this thing called the open API typescript code gen and so essentially all I had to do was pass it that open API specification and it generated an entirely typed client that I can use now in my react application so you can see if I do service dot all of these endpoints are fully documented and I can call them here from my typescript app so all that to say even if you're building your backend with net and your front end
OpenAPI TypeScript Client
with typescript you still have a way of integrating those two and having good type safety between them which is really nice as well so that's your learning plan familiarize yourself with the language build yourself an API follow the basic tutorial and then start going from there now like I mentioned the world of C and.net is really really big so definitely check out the rest of the links in this document where I talk about those kinds of things so one of the things you should be aware of though is there in the past were a lot of different versions of net these days it's the net platform and net version 8 which is what we're using but you might come across old things that reference NET Framework or net core these days we're specifically talking about the net platform now another thing to be aware of is with asp.net there are other ways of building backend applications so they have things called razor Pages MVC and Blazer those are a lot more involved I will say it's a bit harder if you dive into these first before doing something like the minimal API if you're coming from a JavaScript or typescript background because there's a lot more that it entails uh but these are ways to
.NET Versions
ASP.NET Razor, MVC, Blazor
build backend applications as well the other cool thing about net is you can build other things you can build mobile apps you can build desktop apps so I have links to all of that and also game engines where you can use C to build applications there as well so hopefully this is enough to get you going and and get you out there learning CP and.net uh but another thing I want to point out is how we can track errors in our applications that are built in this decoupled way where you maybe you have an asp.net backend and a typescript front end and so we can do that with Sentry so syntax is brought to you by Sentry and they are an error monitoring platform they allow you to track issues in your production applications that are running all of the errors that happen will get sent here they'll get grouped together and you can also see how often they're occurring and when they're occurring and they have a lot of other built-in features as well but one of the features I want to highlight is full stack tracing because if you have a backend written in a language like net and a front end written in a different language like typescript without something like centry can be very hard to figure out when an error happened on the front end how does that actually tie
What is Sentry?
Full Stack Error Tracing with Sentry
in to the back end and centry can do that with full stack tracing so for example let's look at this error that happened here in this app I was building with AET back end and a typescript front end uh request failed with status code 500 and we can see here that uh this specifically is the frontend error if you click on view full Trace we can actually see where this happened in the loading of everything so I have a client side application that had a page load you can see it loaded all of our our assets and everything and then there was that request to the back end here but because I have the Sentry SDK installed in my backend code and installed in my front end code I can now see that hey that error that happened on the front end that's actually linked to an error that happened on the back end and we can see that there was an exception in my my net code here so this is one of the really cool things about centry is not only do you get error tracking but even if you have multiple applications that are all communicating with each other if you enable tracing you're going to be able to see where that error originated and how it made its way to the back end as well integration is is super simple
How to Add Sentry to your Apps
so in my backend code all I have to do is say use Sentry pass in my DSN and and specify all of my settings and then in my front endend code I just say entry. init choose which Integrations I want and it takes care of the rest so it'll automatically attach headers to like outgoing request so that this integration is automatic and I don't have to do anything else beyond the Sentry do AIT so even this autogenerated client here is able to trace those requests to my backend without any extra codes because all I had to do was just add this browser tracing integration so if you're not using cry in production you're Flying Blind and uh it's probably really hard to debug your error so do yourself a favorite check out centry if you go to sentry.io syntax we have a code tasty treats you can get two months free so definitely check it out all right that's all I have for you in this video like I said check out this document I'm going to link down in the description and go forth go forth start writing C Cod learn.net and let me know what you build and leave a comment down below if you are going to learn C or if you have some favorite things about C
Thanks!
Quote it with credit and a link. Quoting Syntax