String Literals - C# Mastery Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we are well on our way to making our first real-world application there won't be many more lessons now until it's releasable and you have boosted your knowledge of c-sharp greatly I won't test your knowledge on passing just yet until we have done a few more lessons on passing as we are making an application to interact with the user that involves a lot of text or strings as those developers call it it's time to learn more about strings in this lesson we will learn about how to create strings from string literals and various types as well as then formatting those strings that is converting the variable types into human readable formats of strings of our choice as well as various ways to achieve the same result finally you will learn about string interpolation this is a brand new feature in c-sharp that even senior developers probably don't know about so get ready to flex your skills in your next interview when impress let's jump into the same code from the last lesson and use it to demonstrate all the ways to work with strings so far you have used string variables by using string literals like so you are even seen combining string literals using the plus symbol which gives exactly the same result but what happens when you want to start using variables and converting them to strings you can see above we have used string literals with the plus two combined more string literals and then to also include a third variable which happens to be a string but what if we want to combine things that aren't strings something we will cover in future lessons is that with the exception of interfaces dynamic and a few edge cases think of every single type in c-sharp as deriving from an object until we cover objects classes and inheritor there is little use in this knowledge for now just know that thanks to inheritance it means that all types such as integers double floats strings and any other tab we have covered by default already have a method called to string this allows converting any type into human readable text this Universal inheritance from object is also what makes c-sharp a unified type system as all types have the same route they all come from the same thing which is a system object class so with that knowledge it is as simple as creating a variable and calling doc to string on it and then including it in the string as we already would now we have a variable called sum int which is of type integer as mentioned we can do dot and then call the to string method let's debug this and see the results as you can see in the locals window some int the name of our variable is of type int and the value 4 and some string 3 where we use sum int and pass it in as a string is of type string and contains the full string we could also just convert the integer to a string on its own and see the result and we can see the integer 4 converts to the string for all literals can have the same methods directly invoked on them without needing to be saved into a variable first this means the above line of code can be shortened by removing the need for the variable and simply placing the literal in line and calling doc to string on the literal this is thanks to the power of the c-sharp compiler we can do this same two string conversion with any type in c-sharp with the few exceptions of interfaces dynamic and a few others so now if we want to make a string that contains text and variables we can do it like this if we debug this and look at the locals window we can see the final string you should understand what we have done here we have two string literals one integer literal and we converted it to a string with two string and we use the plus symbols to combine all three together to form the string I and 400 today the value of all strings joined together so the value of this expression is then stored in the some string for variable because of the equals assignment operator so as mentioned you should fully understand this line of code with what you have already learned once we start combining multiple variables and string literals together this can get very long-winded for example in this example code here it looks very messy the reason this is ugly is that every time we introduce a variable into the string we get interrupted by closing the double quotes adding a plus symbol adding dot to string open close parentheses adding another plus and then adding another double quote there are various ways to shorten this and clean this code up so it is easier to read one of the best ways to shorten their string and to make it visually much nicer is to use string interpolation although it sounds very technical and advanced it's really easy the idea is simple this line of code here you should understand we have covered all the topics this is simply a fake class that I have made to illustrate potentially some code coming from weather data and rainfall data which is nothing more than blank classes down here just for this example and alls we are doing is converting data such as objects and floats into strings and combining them into a result to mimic the same line of code we can do exactly the same thing but this time start the string with the dollar sign this is now an interpolated string if we continue to rewrite this exact statement here using string interpolation instead of having to close the double quotes add + ease and our two strings instead we can simply open and close curly braces directly inside of the interpolated string this now allows us to place code inside of these curly braces in this case we will place the exact same variable data inside the curly braces as well as removing the need for the plus when we use string interpolation we no longer need to call dot to string on things that are not already strings if we provide some information inside of the curly braces and the returning value in this case a date/time offset is not a string the compiler will automatically call dot to string on the value returned some important notes when using string interpolation first if you do end up breaking up your strings into multiple lines like we have here each new double-quote would need a dollar sign in front of it if we place a dollar sign in front of here and move this variable inside of the string inside of curly braces we then use the plus to break the lines up and start a new string below and if we were to place the next valuable piece of information inside without the need for to string you can see this time the text is still the salmon color which means this is actual text that is because we haven't got a dollar sign in front of this string literal as mentioned the dollar sign only acts on the string literal it is attached to if we then break up the string into multiple lines each line needs its own dollar sign if we were to start by writing a string literal and we press ENTER while we are inside of this string visual studio will automatically add the plus the newline and the dollar sign for us so we can happily write strings nice and quickly if we continue to finish this variable conversion off [Applause] you can see now how much easier this is to read than the previous example a second point to remember is once inside of the curly braces spaces do not matter for example adding spaces like this do not convert to actual values inside of the strings we can see in the locals window the string today is has one space and that is this space here before the curly brace and then all of these spaces are ignored that is because the spaces inside of the curly braces are nothing but spaces in code just the same as adding spaces here for example it does not output spaces in the string because this is an expression and spaces around the expression do not matter my preference when doing string interpolation and curly braces is to leave one space either side and to not have the variables bought up against the curly braces this is a personal preference and you can use whichever style you like but my preference is one space after the curly braces now with string interpolation basics covered let's put that knowledge to use and rewrite our console.writeline to use string interpolation as mentioned we start with a dollar symbol in front of each double quote to turn them into interpolated strings and now inside of the string we can simply use curly braces to inject our variable here if we cut this variable get rid of the surplus plus open curly braces and place our new variable we have exactly the same line of code you can see in this example it really doesn't look any better but as you have seen with longer examples the code becomes much more readable as we have not yet covered custom string formatting in which again using string interpolation makes a huge difference and much shorter code you won't see the full benefits of using string interpolation yet we will cover string formatting in future lessons to understand what is going on with string interpolation a little bit better let's take a look at what happens inside of these curly braces inside of the braces the compiler is expecting you to return a value just as it would when you are combining strings in the old way 400 dr. string returns a string date time offset UTC returns a date time offset the benefit of string interpolation is we don't need to return a string we can return any type that derives from an object so that it can have the dot two string method called on it this means for example we could not call console.writeline inside of this code because console dot write line returns void which isn't a value voyage does not derive from an object to prove this let's write the code we first make an interpolated string and inside of here we can just instantly open up the curly braces and write console.writeline although this code is valid it won't work as we hover over you will see because it states cannot implicitly convert type void to object this also hints to the fact that it as an expecting an object or a tab that derives from it which is pretty much everything as mentioned so that it can call dot to shrink however console.writeline returns void which means it doesn't return a value and void cannot be converted to a string it may seem strange to see new syntax so I always like to teach the new syntax by comparing it to old syntax that you are familiar with as we have done by converting this line of code and the console dot write line code between the new and old formats for example to convert this one to an interpolated string we can put a dollar sign in front of the first string and then everywhere we encounter a variable replace the double quote and plus symbol with an open curly braces remove the dot two string applause and the next double quote with a closing curly brace this effectively converts an old-style string to a new style string you will learn more about string interpolation but it is important that you fully understand how the basics work so you are confident with this new syntax to reiterate the key points string interpolation is used by putting the dollar sign in front of a double quote to insert code in a string place curly braces anywhere inside of the double quotes anything inside of the double quotes is treated like code and as expected to produce and return a result the returned result from the expression inside of the curly braces if not a string such as here will automatically have docked to string applied to it by the compiler I will leave you with a few lines of code you might not have thought about but if you understand the lesson fully would realize they are perfectly valid [Applause] remember the code inside the curly braces is treated like code as long as the expression produces a value it will be okay as we can see the some string six evaluates to four which is the result of the expression one times four you can think of it like taking the code inside the curly braces and assigning it to a variable instead and then placing that variable inside of the curly braces if your variable assignment is okay then your interpolated string will be okay if you struggle to understand the concept where you place your expressions directly inside of your strings feel free to place them in variables firstly and then inside of the strings in fact this is the most common way as we can give the name of any expressions a variable name that makes sense inside of the string for example instead of this string looking this long and messy we could take the date/time offset now cut it out make a new variable called today's date and assign it the variable value back inside the interpolated string we could place that new variable name the same goes for the weather we can give a variable name today's weather create that variable and assign it the value and finally the rainfall data we can do the same for here too [Applause] and now look how much nicer this interpolated string is to read this code now is much cleaner we can see that we have today is date and where it comes from today's weather and where it comes from and the rainfall and where it comes from these are key pieces of information to consume and understand and then when we make the string we are not focused on where the information is coming from instead we are focused on making sure that where we say today's date is that we actually have the right variable and when the weather is that we are placing some information about the weather so in fact using string literals is much more common to use variables than it is to directly embed expressions if you are not a hundred percent sure of how all this works go back and re-watch the lesson
Info
Channel: AngelSix
Views: 4,911
Rating: undefined out of 5
Keywords: c#, software, development, wpf, vue, vue js, animation, web design, wevb, .net core, asp.net core, free, open source, git, tutorial, beginners, real world, javascript, html, css
Id: Txvt8us_Wxc
Channel Id: undefined
Length: 19min 54sec (1194 seconds)
Published: Sat Apr 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.