Main

C# Tutorial - Basic - 018 - Strongly typed & Type-safe

================================================== ๐ŸŒŸ Contents ๐ŸŒŸ โŒ› 00:42 : Strongly typed & Type-safe concepts in C# โŒ› 01:31 : Strongly typed & Type-safe concepts with an example ================================================== C# Tutorial from Basic to Advanced - Created by Mojo & Immo In this Series, we teach you C# in three parts Basic, Intermediate, and Advanced ================================================== โœ…The main goal in all our courses is to cover everything in a simple, visual, and practical wayโœ… ================================================== #csharp #csharptutorial #csharpbasics #csharpprogramming #csharpdotnet #csharpcoding #csharpfundamentals #dotnet #csharpforbeginners #csharpbeginners #csharpforabsolutebeginners #csharpfullcourse #datatype #stronglytyped #typesafe

SimorghNest

10 months ago

Hello, everyone. welcome again in previous lectures, when we declared a variable either implicitly or explicitly we told you that its type is fixed and cannot be changed for example, here, we've encountered several errors in lines 9 and 10 because we tried to change the type of these variables but why can't we change the type of these variables? it's because of strongly typed and type-safe concepts in C# these two concepts are related, but they have different meanings in a strongly typed langu
age like C#, every variable must have a data type for example, in line 6, we declared a string variable explicitly and in line 7, we declared an int variable implicitly as you can see, the type of these variables has been determined at compile-time and since C# is a type-safe language we cannot change the type of variables as we tried to do that in lines 9 and 10 ok, now let's illustrate these two concepts with another example imagine we have 3 different containers on the left side and these co
ntainers represent bool, string, and int data types and on the right side, we have some corresponding shapes to these containers these shapes represent the values that the containers can hold ok, as we know, based on the Strongly typed concept every variable must have a data type so, let's declare a string variable named person name and specify its type explicitly ok, in explicit type, when we specify the data type a square container comes in and when we assign a value to it the square shape co
mes in as a value and is placed in the square container ok, now let's declare an int variable implicitly in implicit type, when we assign a value to a variable the circle shape comes in as a value then the C# Compiler checks the value in the compile-time and based on that recognized the corresponding container as data type all right, as you see in these two examples we determine the data type of variables when we declare them this is the Strongly typed concept in C# which means all variables mu
st have a specific type at compile-time ok, based on the Type-safe concept in C# we cannot change the type of a variable let's illustrate that as well first, we change the value of the age variable to 17 at this point, another circle shape comes in as a value and then the compiler checks its type if this shape is compatible with the container the compiler allows this shape to be replaced by the older shape but if we change the value of the person name variable to false a triangle shape comes in
as a value and then the compiler checks its type and as we expect the compiler recognizes that the triangle shape and the square container are incompatible so obviously we get an error this is the type-safe concept in C# which means that the compiler checks the types of variables at compile-time to prevent type errors at runtime all right, in totally, in C# all operations are safe at compile-time because it is both Strongly typed and Type-safe during this course we'll talk more about these two
concepts ok. done, see you soon in the next lecture

Comments