Change language

TypeScript for C# Developers – .NET Beginners Course

TypeScript for C# Developers - .NET Beginners Course

Welcome to the first in a series of TypeScript for C# developers videos, starting with let and const.

In C#, todays most common way of declaring a variable would be with var, so we can go num likes is a hundred and do console dot write line numLikes, save that, run the scripts, and we get a hundred very good in TypeScript var is highly discouraged. So most people would recommend you use let for something like this 100 console dot numLikes and we can run the app. And it says a hundred. Very good. So obviously numlikes can be changed 200 run the app 200 and here we can go numlikes is 200. Very good.

So if we didnt want to be able to change the value of numlikes in C sharp wed use a constant, however, there is a limitation in C sharp, where you actually have to declare the type here. Now you cant change the value of numlikes after setting it.

In TypeScript, its a similar concept, you can make something a constant and then it wont let you change the value afterwards in TypeScript, if you wanted to use a default value for a variable, you could do let num subs.

And write console dot log num subs. So lets see what this prints out. 100 and undefined.

By using let you dont have to provide a value. The equivalent in CSharp would be. Something like this num subs problem here is that you cant declare var num subs. You have to provide a default value.

Then console right line would be zero. Of course.

You cant do the same thing in TypeScript you cant just say that this is a number.

Because as soon as you try to use it, it will throw an error saying variable num subs is used before being assigned. So to fix this, then you would then have to provide a default value.

One more thing they have in common is that you cant, you rely on a default value for a constant in C sharp throws, an error, and likewise consts in TypeScript also have to be initialized so in summary, constants must be initialized and cannot change later and use let for something that doesnt necessarily have to be initialized and can change later.

Lets talk about the primitive types in TypeScript. Lets start with the most simple boolean type in C sharp. This is obviously something like has liked and then you can set hasLiked is true, true, and then do console dot write line hasLiked.

It says true. So far, so good. In TypeScript, then the equivalent would be let has liked is a boolean. Again, you cant just use bool doesnt work , boolean, you can do hasLiked is true. Like so, and then do console dot log has liked.

And if we run it, we get true. So far so good.

We can obviously join the assignments together, like so true equals true, which has no change in behaviour. True. True.

Next up, lets take a look at numbers in CSharp. We obviously have 32 bit ints, which can be something like a thousand. And then we have long 64 bits, which can be something like obviously the number of subscribers to this amazing channel. Lets overflow that 32. And obviously this is picking this up at 64.

Over in TypeScript, this is slightly different. So we get likes, now number is what you would use here. A number can be an integer or a floating point and set this to 1000. We can do let subs now, because this is over the 32 bit limit. We have to use something called a big int, not a long. And when you set the value of a big int, you need to put your massive number on with an N at the end.

And if we wanted to join the assignments again we would just d o it like this.

Now lets take a look at strings. If we have this handsome chap called Ted here, here in CSharp land. We can then obviously do the string interpolation, so we can say chap has likes, likes, like, so then we can do a console dot writeline message. If we run this in C sharp, obviously we get true. Ted has a thousand likes, fantastic. Over in TypeScript land, if we wanted to do this, we would let chap is a string.

Say that the chap is Ted and then the string interpolation in TypeScript is a little bit different. You dont use double quotes. You dont use single quotes. You use a backtick, which for me is on the top right of the keyboard.

And then you can write the interpolated string like this chap has likes, likes. Like so, and then do console dot log message and run the app. True. Ted has a thousand likes. Not quite true, but it does the job.

In C sharp, if you wanted to do an enum you would do enum sub status. For example, and well do this as unknown, curious, subscribed, not subscribed. So we would say that the sub status is sub status dot subscribed, We do a console dot write line, and well cast a sub status to an int, so it can show what number has been assigned. I can run the application and well see that it is assigned to two . Over in TypeScript land. It is actually a strangely familiar. It is almost well. The syntax is pretty much the same.

So we do unknown, curious. Subscribed, not subscribed like, so we can then do let sub status is sub status dot subscribed. And we can do a console dot log sub status. Now, when we run this and TypeScript by default, it comes up with a number you dont have to cast it to an int. But what number does it use? Obviously its using the same rules as C sharp, where it does 0 1 2 3 0 1 2 3. So if we set this to 10, set this over on TypeScript to 10 as well and run the app again, we can see that the result is the same 12 and 12. With C Sharp if we wanted to get the string representation.

To send out to console write line, wed remove the cast. Like, so now it says subscribed.

In TypeScript, there is a secret, array accessor that has been added to the enum type itself.

So if we do sub status and use the integer to access it and run the app again, it now says subscribed. Cheers.

TypeScript for C# Developers - .NET Beginners Course TypeScript: Questions


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Best laptop for Zoom


Best laptop for Minecraft


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method