Learn C# with CSharpFritz - Collections and Generics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
uh says sending data and [Music] no now it says online there we go there we go but yeah there we go all right we're gonna take a look and see why that's not connecting to my existing things that we pre-configure over there on youtube how you doing there friends um my goodness it's been a few weeks um gosh we had the american labor day that we took off last week i was off i had uh took my daughter out to penn state and had her out for a tour of uh of school looking at gosh i'm looking at at schools for for my daughter to to graduate and go to college let me tell you that's quite an experience um but we we're getting there um let me see i'm gonna send a message to folks here on youtube real quick um something wrong with this event now broadcasting at and there's the link all right um so i'm back we're going to be talking more about c-sharp for beginners this is a very beginner-friendly stream we're getting folks used to accustomed to what's going on with uh all the great stuff that's happening with dot net and i want to make sure that that you're successful that you get the answers you need all of the samples that we have here are live and and available for you out on the github check it out at github.com c sharp fritz slash c c-sharp with c-sharp fritz let me show you what that looks like we'll head over to the other side of the code and uh we'll welcome in some friends here so good to see you there we go there's there's my github c sharp with c sharp fritz will get you in and you'll be able to download and work with the notebook that we're going to be using today live on stream there you go there's a link to it in everybody's chat so you can take a look at it let me say hello to folks now that we've got our youtube friends online nitro evil good afternoon to you charles galuli is here let me head back there we go uh our shia hey there i'm doing well riven 9 janescu good to see you surly dev i'm doing well it's the beginning of the week i'm feeling i'm feeling good edu tomasi hello hello just call me a.d good to see you um your bus one hero you like you like the beard thank you uh robert love the hat yes we are penn state um there's a there's a legend that goes around that cheer for those of you that aren't familiar with pennsylvania state university uh university i graduated from um has a very very famous cheer uh right for raising spirit athletic events um where one half of the arena will yell we are the other half yells penn state and the the the legend goes that a long time ago here in the states back in i think it was the 40s um the football team's getting ready to take the field and penn state has a has a player uh who's a person of color and the other team refuses to take the field because they don't want to play against somebody who's a person of color and the the that player says it's okay guys you can go without me says and the team captains say no no we are penn state all of us it's an inclusive statement saying everybody belongs everybody is a part of our community that's a pretty cool thing to to celebrate every time that the the student body that the the that the fans want to get involved and and um i'm very proud to be a part of that and they had a big win this weekend big big win in college football this weekend over uh our friends in from auburn um so let me keep rolling down here how's it going there ark adonis adipose good to see you a mall it's been two weeks it has indeed uh abhishek what about python we're we're not touching on python today we're we're working on c it's called c sharp with c sharp friends we're working with c sharp um nadine paulie good to see you uh that's right nitro evil we are penn state john a atto good to see you uh it is it is a nice story it's a legend i don't know how it's um i i don't i don't know if it's truly been validated but that's that's the story that that we've been told the 30-second review of collection concurrent bag not going to get that deep um but i will take that offline and and we can discuss further i want to i want to get in and get it get to the basics of arrays uh lists dictionaries and how we can use those and start to use link to interact with those and get folks past uh past those basics here um let me get some music playing in the background of course i like to have some music playing in the background while we're writing code and learning together um and today i'm gonna go grab the uh stream beats where'd it go i want the lo-fi right synthwave edm yeah all right we'll go with the synthwave playlist today now let's skip down a little bit i want to go somewhere down here this is the synthwave playlist from streambeats this is music that's designed to be royalty free dmca free you can use it wherever it is you might be streaming you can listen to it it's a nice set of uh tech beats that you can listen to um for free there's playlists for apple music uh amazon music spotify check it out streambeats.com thank you so much to harris heller for making this music available for us to listen to today so it's kind of important to to pull together and uh and have some music that we can all listen to um so kind of groovy just have it there in the background um let's see what else where are donations going today we don't do donations when we're over here on the visual studio channel um for those of you that don't know maybe you're watching over on youtube maybe um maybe you've you've seen some of my other streams uh or you're watching on visual studio stream over on twitch or maybe you're watching on learn tv um when when i stream over on my channel on twitch um if folks do want to donate if you do want to contribute subscribe cheer uh we make uh donations to a two well-known communities uh charities that are going to help various folks in this quarter the third quarter 2021 we've been donating to women who code we've donated to black girls code and vets who code and all kinds of other organizations over the past two three years really happy to be able to pay it forward and support folks uh brad bradfield phillips greetings to you in cape town bill gates uh he's still around a little bit not too involved with microsoft anymore tasmania is where paul's connecting from hello hop in the cloud good to see you getting some coffee into me today um so he's um i don't mind answering that question typically i like to stay around the the topics that we're going into but last week.net six release candidate one was released you can find that you can find out more about that at devblogs.com i'm sorry devblogs.microsoft.com.net um and i will bring that up real quick put the.net on the end if you don't mind there we go and yeah yeah let's head back over there um so i'll answer on maul's question there uh mwj computing go green go white uh you your michigan state team did very well this weekend as well um here we go announcing dot net six release candidate one this was announced last week for those of you that are keeping track you want to follow along you want to learn more about the next version of net.net 6 being released in november as part of net conf our annual virtual event in its 11th year um the.net team puts this on there's a full day of community content on the third day of the event but we'll be releasing uh net six which is a long term support release you'll get at least three years of support with net six that'll be released in november now to to the question from a mall a little bit ago um dot net maui the next um here we go the next version of the tools that we're making available for native development designed to allow you to build for windows mac os ios and android are going to be released as a preview at at the time it's the net 6 release so they you will not have a full release it's taken a little bit of time for us to get there it's not at the quality bar that we want that that we want to make sure you expect from us when we deliver high quality programming frameworks and tools so we're going to delay it slightly there'll be a preview release still available we're continuing to iterate on it we're continuing to work on it you can find out more about what's happening with net maui how it's progressing you can still develop with it we're still iterating and releasing and working on this in the public you can find our work on this on the github repository it's all available out there for you to be able to interact with ask questions file the issues and more but you can find out from uh scott hunter here he writes a blog post all about what's going on with dot net maui i will share that link for you out there uh cross platform user interface that's right it doesn't quite work like like wpf it works more like xamarin but it's still xaml based that you can work with um taking a look here uh looking at some more of the comments some more of the questions coming through um when is visual studio 2022 officially released i don't think they've announced it yet still so i'm not going to comment on that because they haven't announced it um they do have a goal when they want to have it announced uh when they want to have it released um if they haven't announced it i'm sorry i can't disclose that um we can certainly go back to the visual studio blog and this is where the team releases their blog post the visual studio blog um and i don't think yeah preview 4 came out beginning of last week so there's the link for the blog here you can take a look and see more about what's happening with visual studio but i don't think they announced the the final release date yet um and i will i will save that announcement for their team to be able to announce um and and mads christians in here right there so um how you doing there astrid good to see you uh net six entity framework six dot net maui uh updates to blazer all coming as part of that.net 6 release yes yes uh what ide editor do i use on all of these uh all of my streams i use visual studio 2022 for the purposes of these streams for the next few weeks as we're working with dot net notebooks to teach and learn more about c sharp we'll be using visual studio code as part of those uh as part of these uh sessions that was a question from paul watts over on youtube all right i think how you doing there thin dog good to see you so that's more information about visual studio preview 4 is available you can download it now you can test it out completely free of charge you're welcome to download and interact with the that preview version of visual studio dot net six always dot net is always always free all the tools doesn't matter what editor you're using visual studio visual studio code your own editor maybe you want to use jetbrains writer maybe you want to use vi maybe you want to use emacs or or something else brackets whatever sublime sure notepad notepad really all available for you to use completely free of charge you can build and work with dot net on all of those editors you started out in notepad thin doll oh my goodness iced tea tv hello welcome so good to see you you wrote pearl well i'm sorry i'm sorry so we're going to be over here we're going to be working in this github repository particularly down in the notebooks and we're in notebook 0.105 today collections and generics i will share that link so you can click through follow along you can clone this repository run it locally open it in in github right you can actually just hit uh i believe you can just hit period here just type hit the dot key on your on your keyboard and it'll open in visual studio code in the browser and i believe it will install and run the extension for you for working with that notebook there it is so right yeah it'll run these live so you don't even have to install anything just hit the period key while you're looking at that github repository it'll open it in github and you can work with it no problem right here and follow along so um really cool stuff but i'm going to be working over on the big screen and we'll be uh taking a look at it that way so let me go back over to there um how you doing there chanescu good to see you uh do i do a tutorial with discord.js no this is a beginner's lesson on c sharp with connection collections engineering so um all right i think i'm just about ready to go over to the big screen and get us up to date there we go how's that sound a little bit different because i'm on a different microphone but i'm gonna head over to the other screen and we are going to be all set for yeah for our lessons on collections and generics with c sharp all right let me head over there and we'll get things started all right give me one minute to get over there with my uh with my coffee here gotta head over do the walk do the walk through the uh the thing here um oh one second i got my uh my headset turned around and not not plugged in properly hello hi we're over here now see gotta have that cup of coffee it's it's developer fuel that's what coffee is right here right there um all right so i am going to keep i've got uh i've got chat open over here so i can see what's going on i can also see a little bit of my background over here um let me take a quick look i'm i'm seeing that the the green screen is uh i thought i had it adjusted properly let me just tune that just a smidge so it's the beam that keeps on going oh yeah absolutely there that's better now you can't see that all right um walk wall clock i know right hop in the cloud says i apprecia i appreciate maui being delayed i've been following.net maui it's going to be very cool to understand microsoft wants to make the experience of using.net maui from day one of release as awesome as they can thank you yes we do want to make sure that it it's a valuable tool for you so we're gonna we're gonna do our best to make sure that everything works the way that it should day one and there's a couple challenges that the team is having and um to make sure that everything is delivered properly they're they're giving themselves a little bit of a delay here so they can get things done so as with all projects we have timelines and milestones and just didn't quite hit the milestones that we wanted to that would be appropriate for that november release you still love wpf bus one hero well all right that's that's perfectly fine gotta get that lighting even i know right now now it looks right here now we're in good shape all right today we're talking about collections generics and we're going to take a peek into link all right so collections generics i hinted at this in some of our previous streams i showed us a little bit with some for loops and and in going through and working with a a collection of numbers a collection of things how to go through a loop a for loop a while loop a do loop and how to interact with those and and do those looping expressions and go through and and discover how to work with one item at a time in that set okay that's that's fine but what can we do with a set of objects with that collection of objects an array is a very common structure in many different programming languages and frameworks and that's it right that's a one-dimensional matrix that has just items laid out in it that you can work with and we'll we'll see more about how an array is is just the beginning of how we can work with collections because we also have other collections like lists and dictionaries and hash sets that we can work with inside of dot net that each have different features different functionality that you may choose to work with but they all come back to an innumerable collection they all are based on that same innumerable collection and we can take advantage of that with a technology called generics to make it even easier for us to interact with those objects more on that as we get into the lesson let me take a look at chat thank you john for the comments yes i've got to support my nittany lions you know what i'm saying here gotta support those those blue and white folks in state college pennsylvania these releases are like rocket launches failure is not an option curious drive you're i i wouldn't disagree with that um let's see adrianmonk says now that.net is unified we will never talk about.net standard never again am i right um i wouldn't say never again we'll talk about.net standard it's something that we want folks to um as they no longer need to support net framework or xamarin um or those older technologies that are are going out of support.net framework will stay in support but if you don't need to work with net framework you won't need to work with net standard um we want everybody to be on net5.net 6. that's the goal but if you do need to support across that into.net framework you will need to work with net standard um converting as much code as i can to net standards you can change the front end as last step totally agree will i cover immutable collections i wasn't planning to um i wasn't planning to cover immutable collections i i can add that in at the end give a link out to that we can go show how those work um are she with a question here on twitch what's the point of storing objects in a matrix so if you have a multi-dimensional array a matrix right and the the simplest matrix is going to be two dimensions if you want to store things in a two-dimensional matrix they have different those objects are residing in memory and they may have different different properties based on where they're residing you'll see a lot of this in um in game development where they need to manage a bunch of different objects in memory that are all interacting and doing different things at the same time so that's a a scenario where you you're going to have lots of different things to manage and you may want to have different um different dimensions that things are residing in for different purposes so that you can easily find those things and work with them uh let me see here i don't see too many folks from youtube chiming in here how we doing youtube you still are we still connected over there let me know um [Music] let me see i think we're all right now looks like we lost connection to youtube did we lose connection over there no i it's it's come here you uh yes we do yes we still have connection good good good good yeah there we go all right um fantastic oh awesome there's some youtube folks chiming in thank you so much uh good i was i was a little concerned because of how how the the connection didn't start quite right for today all right so let's let's get into this let's get into the topic here um i am not familiar with that book zeus zeus just got started with collections in the book c sharp programmer study guide no i'm not familiar with that book uh can you drop me a link on on twitter uh same name on twitter c sharp fritz drop me a link i'll take a look um you're in listening mode not a problem um it i i look for the the connections from the the uh two different um chat rooms and i saw a lot of twitch and didn't see anybody connecting in from youtube all right so let's talk about this collections generics and a peek into link so um collections there's a bunch of different collection objects that you can work within.net and i mentioned some of them let's start with arrays here so let me zoom in just a smidge so arrays are reference types and the simplest of the collection types can be declared with one-to-many dimensions and can also be declared jagged so it doesn't necessarily need to be the same the same depth the same number of elements in every dimension you can simplify their arrays are simply declared with a type and square brackets this should be simply there we go um so there's the square brackets defining the size of the array initialized with a new statements and curly braces containing the initial values of the array so you can declare an array and put those first items into it all on one statement so if we take a look here now i'll zoom out um you can declare look i have an array of integers by saying int square brackets numbers so that's an array of integers called numbers now um it doesn't contain anything initial here as long as it wasn't assigned yet so int numbers there's nothing in it so i'm going to say display if numbers equals null array is created and it'll say true there now if we recreate the array using square brackets and we define a size a number of elements in it right so right here int three it'll tell me that there's three elements in there and the ray is not null here now um there is a length property that will show you the number of elements in the array so we can say array size numbers.length and we can also declare here's a here it is declaring with initial values so there full array of numbers int one two three right right here so we're going to put those initial values into the array full array of numbers and we'll display that information so array is created true the array is not null in that second block because it it has it has elements right the second display array is null right there's actually elements the elements are null but it has three elements in it and it has right there it is three elements and the full array of numbers it has a full length here and i can actually add another let's add one more display here for full array of numbers and run that again now notebooks when you say display and you pass in some sort of object go display this thing if you pass in a collection dotnet interactive notebooks know to display as a table like this so there's my index 0 1 2 all arrays are base 0 indexed they start from 0 and the values are 1 2 and 3. so this way you'll see developers like to count starting from zero so all right that's an introduction to arrays let me take a look at the chat room let's see anything else that's that's being asked here uh can you tell what is asked at a microsoft interview no i cannot sorry can we get some love in the chat for a raise yes please it's bananas good to see you um when you need control but it's open to lose versatility versatility is it possible to create an array of type object asks curious drive yes you can create arrays of type object so let's create another array here um you know what i'm going to do it in let me create another code element just below this uh come here where's my plus i should have a yeah right there so i can create an array of type objects so we can say object uh and this is my curious array right so that works i've i get a green check that it did create that and we can start to put objects into it so let's say this is a new object array and i can put different things into here uh let's do this uh and date time now okay um and if i display that array i get because it's an object array when it goes to display that object that's in the array i'm uh the net notebook is giving me the type of each object that's in the array and then it's giving me the value cast to a string and output here so you can work with objects as as that most abstract reference type that you can use inside your array absolutely and put all kinds of different things in it um arshia asks can arrays have children like integer of size three only has three values or it can have an array inside an array yes you can put other collections inside of an array so but at that point you're doing a multi-dimensional array right we can we can start to declare that as a different size now if i go down here i can reference the items that are inside the array there it is i can reference the items inside the array with that index number you saw the index number down the side here i can reference them by using the square brackets this is called the indexer of that array variable i can reference it and it will give me just that one value so if i reference the zero width item in here it will give that back to me and i can also set values on the array by interacting and saying the zero with item equals five so full array of numbers when i interact with it here item zero starts off as the value one but i assigned the value five here go display again and now i have the zero width item is five so we can manipulate the contents of our array what we can't do is add items to the end of the array okay once the array size is defined that's how big it is it's three items in this case you can't go and and shrink it or resize it by just saying add or remove items okay it's going to stay that one size and that's one of the benefits and one of the cons one of the drawbacks of an array because you may want to dynamically build the size of your collection let me take a look back at chat answer some questions the display function is interactive notebooks yes uh not going to talk about i async enumerable no it's a little bit further advanced than i want to get into is there something like the spread operator like star in python to unpack the objects from array one by one um i'm not familiar with the spread operator it's i mean you can do a for loop or a while loop to interact with it i'm not quite sure what you're trying to do with a spread operator that's right curious drive the string data type so in c-sharp uh in.net the string data type is an array of single characters so just like we did here with these two elements i'll add another block here so if we define string uh my string equals we'll say fritz we can reference each of the characters in that string using the indexer so if i say display my string uh and let's say uh zero one two three four and we say display four i get the the character zed okay because it's the fifth character in the array in the string fritz all right so i mean this gives you a way that you can manipulate strings by interacting with the elements of the array the the character array that is the string you can do that there's other string tools that are available that allow you to work with it a little bit easier but this is available to you okay um additionally there is a uh there we go two character array method on strings and it will actually turn it into an array and there you go it's displaying here's each of the elements in that array that makes up the string fritz okay so good question thank you so much for that curious drive over on twitch um all right moving on then so you cannot interact with array values outside the size of the array so if we try and interact with with numbers that are outside right so there's only three elements in our array full array of numbers so if we try and work with the fifth item in the array we get a big old error here index out of range exception because you tried to work with the the fifth element and there's only three in it so it it breaks it throws an exception to say oh can't do it can't interact with that now here's we had a question about multi-dimensional arrays so if you want to work with right two-dimensional array you can put an array inside an array this is a little bit more efficient way to do it um so if we have a matrix and we say that that matrix is three by two i can define here's my three elements each with two elements in it okay so this is multi-dimensional array and when i say display matrix dot length the length is the number of elements in that array well the array has six elements in it because it's three by two okay and we access elements in that multi-dimensional array using a comma to separate and specify each of the dimensions we want to go across to find things so if we want in the zeroth dimension the first element the second i'm sorry in the first dimension the first element and in the second dimension the second element we're going to get the value 2 because it's right there all right so you can you can resize the array it's not something that that you want to do it's very it's a little bit destructive there's a lot of memory thrashing that happens to do that so if we interact with my numbers we allocate it as three digits here and we try to say well add another element to the end just put another one on there there is no add capability to do that so you need to do this resize interaction so we say array resize and you pass in a reference so work with this my numbers array that i defined and resize it to size four let's assign in the fourth slot zero one two three it's the fourth slot fourth element let's assign the value for and display my numbers oh i gotta this didn't work come here you there we go didn't compile so now this will work there we go and now i have a fourth element all right so you see we have to do a resize to make it the size that we want and then we can interact with it so by default interacting with an array is easy to do easy to to put things into it get those values back out of it but once it's declared of that size you need to do things like this to change its size we have other collections that are better better optimized for that type of interaction of dynamically adding and removing elements from the collection things like a queue or a stack those you're dynamically adding things to the collection and removing things from the collection after compile time that's okay um after declaration time i'm sorry you're changing the size of it let me take a look at chat um yep arshia i just showed that um so it sounds like you probably asked that question right as i was showing it uh you once tried to draw a 4d array but your brain exploded well isn't my numbers already a reference type why was it passed with the ref modifier ask amal over on youtube great question it's passed with the ref modifier here it is a reference type and we're passing it with the ref modifier here to ensure that the reference of my numbers is being changed that's being modified right i can't i i can't pass it like that you must use the ref keyword to indicate that it is being passed by reference and it's going to be updated it's going to be changed there um dj squared asks is there a performance advantage to using an array over a list or another collection type in college professors really hyped to raise however professionally i've pretty much never used an array there is a little bit of a performance benefit to using it it's because you're you're not allocating and reallocating memory for changing the size of things so you're forcing things to not be um allocating and reallocating memory um as as those collections change size so it can be placed in one location in memory and it doesn't move around because in in the memory uh heap because it's it's not changing we're not reallocating things okay when you need to add new things to it when you do this resize it actually has to take it off the heap change the size of it give put all your stuff into it and put it back on the heap and get rid of the old one that's a very complex operation that if you have a lot of things going on in memory you have a very memory intensive application you will feel performance impact of that but if that al array is one size it's never going to move in in memory right it stays in one place the the application knows exactly where to find it in memory so you don't have this thrashing that'll occur all right and consequently then also the garbage collector isn't running a lot to go and clean up these things that are no longer being referenced arrays are very cash friendly yes it's bananas yes yes yes so all right we are moving through this quickly look at that look you can't see this look at that i'm already what about a quarter of the way down through this today oh man we're doing well um all right so if you reference the ray somewhere else in your code that reference will not be resized as well yes so arrays are reference types and references to the prior object are persisted so if we let's resize my numbers again this time we're going to make it five elements and we'll put the number 100 in the fifth element there and display my numbers now we captured a reference to my numbers in reference to my numbers first it's a reference type so when you assign it to something like here right all that we're storing is a pointer to that but when we do this were being passed back a new array so 1 2 3 4 100 is what was in my numbers but in reference to my numbers it's only one two three four because it still contains a reference to the old array right so to that question that we had just a few seconds ago right when you reallocate when you resize an array it creates a new array of that size so scrolling back up here it created a new array of size five fritz's new array whatever it is it's doing it behind the scenes it puts it into memory somewhere and returns it into the my numbers variable okay so now my numbers is a pointer to our new array however reference to my numbers is a reference to the old array it's still out there in memory because this has that reference does that make sense so this is where we start to run into memory leaks because you've got a reference to something that and and it was destroyed somewhere else but because you have that reference over here somewhere where you might not remember where you were referencing it it never gets cleaned up yeah that thing that you were doing over there that goes away but over here still persists still hangs around that might be a good thing for you might not depends depends on how you want to use and interact with memory um taking a look back at chat let's catch up here um johnny b cat on youtube asks how can we reassign a value in the array so right here we assign the value 100 to the fourth element in the array right well it's actually the fifth element right because we count from zero so it assigns it to the fifth element real easy if i want to um if i want to add or change that right so i can just say uh right it was my numbers and i'll say the fourth element equals uh um i'll set it to the value 28. all right and uh here let's say display my numbers so now the first element here is my numbers and you see it's now 28. so to reassign a value in the arrays just set it equal to that new value just like you might expect when we're interacting with um other other objects and other properties so yeah memory leaks here we go it's a new array but the elements maintain the old reference no they're copied they're copied fernando so um lakshen asks can i explain stack and heap memory um briefly we we did discuss this in a previous stream but i'll briefly describe it in.net we have two different locations that we store things in memory the stack and the heap the stack is where we store our value types numbers booleans characters are stored on the stack um very quick interaction with it objects these value types are allocated and deallocated and removed and cleaned up and kept and it's being kept very neat and tidy reference types classes strings dates um arrays collections like this are reference types they're placed on the heap and and the heap is is a large area of memory where things are allocated and deallocated when things are no longer used and they're deallocated our application says don't reference that anymore there is a garbage collector in.net that will come through and collect and clear up that memory release that memory so the rest of your application can continue using it so it's very important that we optimize for when we need to optimize for performance that we keep our memory usage and our allocation of things for the heap our reference types as small as possible where you may have seen the impact of this is when we think about net 5 and net 6. starting with net core 3.1 there was a new type that was brought into um into.net called span of t or memory of t these two types instead of instead of being a collection that that sits out there and sits on the heap and and takes up space and what this does is it instead provides a pointer to somewhere in memory where our data is allocated so it doesn't allocate new data so we end up creating these little value types that reference where this data was allocated starting with net core 31 but more efficiently seen in net 5 and you're really going to see it in dot net 6. behind the scenes in the in the framework for net we're using those spans and memory collections to generically reference where these data types where these uh this content lives in memory so that we can interact with it faster we're not reallocating memory when you think about how a web server runs a web server passes a lot of strings back and forth the the web browser that you're watching this video now in is made up of html a bunch of angle brackets and string content that declares to your browser how to format the page and there's a bunch of strings that are passed back and forth between the web server and the browser to indicate what type of content it is how fresh it is if it needs to be re-requested the the format of it hey this is a video you're watching you need to format it and present it as a video all that information is being passed back and forth as strings well if when our web server receives those strings if we're not reallocating a bunch of memory to work with those strings our web server can run much faster because it's using less memory and that's part of the basis very simplified basis of how.net has the fastest web server available i hope i hope i covered that well let me know chat um so dj squared going back to this sample here let me go back dj squared asks step one up here reference to my numbers points to mine the initial collection of my numbers yes step two we resized and it points to a new place where this new my numbers collection is is going to live in memory however you're right reference to my numbers still points to that first location that first address right this is pointing to some address some location in memory 123 main street is where where this is residing in memory when this reallocation happens here the resize it's actually allocating new space for that my numbers and it might be on four five six main street right it's a different address in memory right one's at the beginning of the street the others a little bit further down the street and it's somewhere else well it the the application returns here that okay here's my numbers it's all resized for you it's got an it's got an extra floor in it so we have that extra element that we can we can contain in our array and oh by the way my numbers now lives at four five six main street it's somewhere else but you're right reference to my numbers still thinks oh i know where this thing is reference to my numbers lives on one two three main street it still lives in that same first location even though my numbers is actually a little bit down the street at 456 main street so you are correct it just points to somewhere else this points to somewhere else but reference to my numbers still thinks you live at 123 main street so right it hasn't gotten that change of address form let's see can i give an example why our code should not reference an object anymore don't we need all the code to be available for it to work yes we do need all of our code all of our things in memory to be available to us you're right however when you finish a method when you're done interacting you're done doing your calculation interacting with and and figuring out what it is that you may be doing in a method at the conclusion of that all of those intermediate objects that you created in your method you don't need them anymore they're done they're they're no longer going to be referenced they go out of scope because the method completes there nothing references them anymore because the method is done at that point we want to clean up we want to remove those things from memory because the next time your method runs it's going to go and create new objects to go and work with and those need to be somewhere else so where the method ran the first time and it allocated its objects go clean those up because the next time it runs we're going to allocate again so we want to make sure that those are cleaned up and no longer being referenced and not an issue right because if they're still hanging around you might not get the same the the expected values you want it because the old objects are still hanging around all right good questions thank you chat room all right good let me move on then um uh thank you uh that dark blue is hard to see here stack last thank you so much you're welcome uh is that lakshan you're welcome um can i print out all elements of the array ragnar on youtube asks can i print out all elements of an array without a for loop um there's a couple ways you can do that so here inside.net notebooks the display method here outputs those elements if i wanted to display those elements without a for loop i can do something like a string.join so i can do something like this and i can specify the separator that i want so let's give it a comma and give it the array that you want it to output so i'll say my numbers and when i run that put a display before that fritz otherwise it's not going to do anything there we go so now it outputs it as a comma delimited array there's other things that we can do to interact with our array using link the language integrated natural query more on that coming up a little bit later because there is so much we can do with that for each is one of those items in link that's right canon ade on on twitch jumping in with that um so and it is you're right bananas it's bananas over on twitch says there's always a loop hidden somewhere while i'm saying string.join here underneath the scenes it's doing a loop there's a for loop there that it's doing don't tell anyone it'll be our secret just you and me and the rest of the internet all right moving on don't tell anyone don't tell anyone all right it's it's just between us all right just between us friends here okay um moment on youtube says uh printing too much data using four or four each loop will it cost too much memory um no it won't cost too much memory printing printing too much data no it'll use more processor you'll use right more your processor will um act on more of your data yes all right i'm going to move on and let me scroll down here we did that reference so we can do a remove with that same array resize and it eliminates elements from the end of the array so if we do array resize my numbers to three instead of it being four elements now it's three and it's down to those three elements same thing just resizing you can resize up and add more elements you can resize down and remove elements and they come off the end of the array so we can't go and remove the two in the middle you can't do that right so when you're working with arrays you don't have that flexibility to poke around and remove and interact with the elements in the middle of the array you're only adding and removing on the end the last element all right hey straxor welcome um it is pretty efficient that's right so a 3d poly wraith there you go all right so arrays are enumerable that means that you can count them right you can go through and do a step interacting with them they implement the i enumerable interface so you can you can iterate over those contents so you can do things like four each right and we we know what fours are in many other languages and we saw looping constraints uh i'm sorry looping constructs previously so we can do a four each when we work with a collection that is innumerable so we can say for each var i in my numbers and display that when we do that it displays each one of those elements um that should be like that yeah let's see that didn't that didn't do what i whatever so we can work with the array fill method here and we can fill our array with values so we can if we allocate a uh an array and we want to call it my1 array we want to fill it with the initialize it with the value 1 we say array.fill give it the name of our array and what you want to put into it once and we'll display that array and we get our array of three ones so may be valuable to you if you're and right you want to initialize a collection you want to make sure that it's it's got some wide number of elements and they all default to they'll default to zero perhaps right maybe you're you're um setting up a baseball scoreboard and you want to default every inning of the baseball scoreboard to the value zero sure you would do something like this to make sure that everything starts with zero maybe baseball scoreboard isn't the best example because maybe you want null in those in those innings of the baseball game when that haven't taken place yet so um your choice right different ways that you can do things hash tables and sorted lists are two other types of collections here that you can work with but these have keys that you can interact with so a hash table is sorted based on the hash the hash hash sorted based on the oh look at this we gotta get rid of that that shouldn't be in there um yep get rid of this in the so that it goes to each value where'd it go that contained stored on the hash hash there we go that's better sorted based on the hash of the keys and a sorted list is sorted based on the key value so what if we had a collection of um file extensions key value pair sure so we can have txt we know that's plain text mp3 is compressed music jpeg let's call it a jpeg compressed image now if we create that as a sorted list it's going to sort by the value of the key so we'll get jpeg mp3 text because right alphabetical order j m then t if we change that instead of being a sorted list to being a hash table well now it's sorted based on the hash of that now for our human eyes it looks weird and out of order right that that doesn't look right that it's text file then jpeg then mp3 that's that's not alphabetic order that's not something that that our human eyes are going to look at that doesn't make sense to us but when we think about the hash and we think about finding elements that are the same in that collection dot net behind the scenes does that by calculating the hash of an object so interacting with a hash set is a lot faster for net to do compared to compared to us where it's going to have to go find well all right this is a j for jpeg go find that in the list it can calculate the hash very very quickly and immediately find that location in the collection let me take a look back here at chat i see a couple more questions coming in here what features does an array have compared with a collection of i innumerable um so right the array has the array has these abilities to interact with the um not just the length but also resizing you can go backwards and forwards in the array right i can randomly choose which element out of the array i want where an i innumerable is a forward only collection all you can do is say give me the next element in the collection which for all these other types of collections like i'm sorry hash table sorted list that's what hash table not hash set hash table sorted list dictionary we're going to see in a little bit generic list all of those when you're enumerating through it it's just give me the next element in the list there are other ways to go and interact with these and grab values at specific locations inside that collection and that's going to be part of the reason why you would choose to work with an array compared to a list or a dictionary or a hash table or a sorted list they're all used for different reasons okay good questions good questions how you doing there jeff brooks welcome in so is a sordid list is a built-in type that's correct chris jones yes what is the difference between a hash table and a key value pair um and i'm sorry i i i can't read the the characters in your name um a hash table is is a collection of key value pairs right we have a a key text we have a value plain text the hash table is a collection of these elements it's multiple key value pairs so [Music] right it's a hash table is composed of key value pairs and we'll see key value pairs used in other types not just sorted list so you have different places that you can use that key value pair technology in collections in.net and and like i was saying how they're stored how you interact with them how they're presented the order that they're presented varies based on those different types of collections and we we saw here as a hash table these are presented in this hash order as a sorted list they're sorted based on that the text of the key we have other types like stack and queue that are sorted based on the order that they entered or the order yeah based on the order that they've entered the collection a stack is first in last out so the last element that you've added to the collection will be the first element you'll see this in a little bit a queue is first in first out right just like you might queue up at your favorite uh your favorite restaurant wait to get inside wait to be to be assigned a table or whatever or at the local coffee shop you're cued up first person in line is the first person to receive their coffee at least that's the way it should work so what's the difference between a dictionary and a hash table s uh tg on youtube uh let me get there in just a little bit um rick has been a developer for two years uh rick's joining us on youtube and he says these videos always teach me something thank you so much rick that's very kind of you i i appreciate that you love my videos thank you so much um can hashtable allow you to have a duplicate key asks uh chang liao scott over on youtube no so if i add another entry to my hash table here right so right not only do i have plain text i have fritz's cool text and we execute that item has already been added and that's not just a hash table that's that's just a key thing right you can't add the same key multiple times even if it's a sorted list sorry item has already been added let me scroll that up so you can see it right so you'll get an exception that you added this thing with the same key so good to know good question thank you so much for that question chang um really appreciate you pointing that out that yeah you can't add the same key twice the key is unique within the collection so how you doing there rambling geek joining us over there on twitch good to see you all right let me i'm gonna comment that out and i'll add a note above that you cannot add the same key twice cool oh sure enough it's the next one here um fine [Laughter] uh that's what i get for not scrolling down i completely forgot that oh yeah that's the next sample so compressed music if we try and add it a second time here right there it is mp3's already been added and it's compressed music so the those key value pairs hash table sorted list are enumerable i innumerable so i can do the four each and go across those and there they are because i've referenced them they are dictionary entries inside that collection right so the collection returns to you a dictionary entry object and then i can reference the key i can also reference the value and get the value back so neat way that you can interact with the entries of that if i don't reference the key or the value i get it displaying each one of those elements so i'll let you play with that you can you can run these live in the um github repository just hit the period key open up the this notebook ticker with this see how those things work together go ahead and break things it's not your machine it's all in your browser and you can interact with within with this.net interactive notebook any way you'd like you're you're not going to hurt anything you throw errors that's okay no problem we'll it'll tell you how to fix it it'll tell you what the difference is here and how we can work around those things um and of course there's always lots of great folks on youtube on twitch on the.net and visual studio channels dot net on youtube visual studio over on twitch and of course out on twitter that are happy to answer your questions if you run into issues and you need a little bit of help um can hashtable be implemented as i observable uh there's a different collection type for for that the observable types uh you're gonna be looking at like reactive extensions to be able to do that and i'm not gonna get into that um alex asking am i going to get into the concurrent collections no i'm not going to get into concurrent collections i'm trying to stay try stay a little bit beginner level but i have had enough questions about about concurrent and immutable collections um i'm i may add to the bottom of this notebook when when we finish here offline i'll add some content um what's the difference between the index operator and the add method um excuse me pardon me um let me see so there's the array so when i was using the index to interact with this right so there we go right so that's using the index to go get that um if i call right if i call add here because this is a sorted it's a sorted list hash table does the same thing now unlike an array that is fixed in size now i'm adding a new element to the end of the array so now if i add uh mp4 right did i not have mp4 in the original right so if i add mp4 um let me leave the can i uh rats right let me leave this here mp3 so that still works but if i say file x dot add i can add a new element into the collection um and if i call this a compressed video uh rats you know um i need to display here because there's more going on there we go so it added to it and if i say display uh file xd it's already been added so where did it go there we go so now there's mp4 in the collection now so write a sorted list a hash table these are these are collections that you can add and remove from you can interact with them and they do land in different locations in memory um so the index lets you go and grab specific items in the middle of the array the add the remove changes at the end of the array how you want to interact with it um actually remove i think you can remove from anywhere in the array also right you can remove by key right come here you right uh there we go so i can say remove i can say remove at and specify an index but i'll specify the key that i want to remove mp4 run that again and now mp4 is not in the collection so i've now got random access to be able to interact with it with these types of collections okay rick has a question here um will this video be uploaded to your youtube channel was a bit late um rick so this video is being broadcast on youtube it will be saved it will be in the playlist on youtube so for all of you that are watching out here if you want to go back you want to watch this later you want to share with your friends you want to wake up the dog and say hey you got to check out fritz teaching about c sharp i i'd love to see a dog that knows c sharp um youtube.com.net look for the playlist uh c sharp with c sharp fritz and you'll see there's i think almost 50 videos there now with uh with me teaching all about c sharp so including this series when we ran it for the first time last year so um i didn't want to scroll that mouse the other mouse um sharaf on youtube asks i want to know if iterating over an array is always faster than a list of items the same size and if span is faster than array um a span will be faster than array because it's a value type pointing in memory so you'll get a span interacting a little bit faster for most folks who are dealing with a reasonably small size of of elements 10 100 1 000 elements you're not going to see a performance difference you're just not millions of items you'll see a performance difference millions of items concurrently you'll see a performance difference for most folks who are dealing with the the list of three elements in an array you won't see a difference you won't see a difference at all it really it takes significant pressure to see that most folks who are writing typical interactions with a database for a website or a desktop application or a mobile application don't worry about spam and memory it there are other places that you'll be able to optimize your application before you need to worry about those data types don't worry about them you're you're not at that there's there's always going to be bet places to tune your database interaction to tune your interactions with the microservice before you need to get down to those micro optimizations of tuning how you're allocating memory it's it's something that is the last thing you should be optimizing not the first um the index set operator oh index of um no you you're not going to see they're pretty much the same can you use remove without any parameters asks or shia um so if we just say remove like that nope you need to give it you need to tell it what to remove you actually have to tell it go find for a sorted list for a hash table you need to tell it which one to remove all right um so let's talk about the next one here cues oh johnny b cats has already shared my link at work thank you so much um yeah you can take a look at what the il is doing yep um yum yum rocks on youtube points out uh you can take a look at sharp lab if you want to see exactly what's happening under the covers you want to see exactly how memory is being allocated and the processors being used you can do that out there but for 99 of folks that are building applications you don't need to worry about it there's other ways to optimize your code before you get there all right a dog that does c sharp uh jens adria on youtube says a dog that does c sharp would be a c sharpay ah see i i like it i like it um effective query writing goes a long way absolutely any courses for the c language no i'm sorry i i do not have any courses for the c language um you're a beginner in it kushal welcome when to use a for loop or a for each loop in a collection um honestly for loop you're only going to do if you can randomly access an item in that collection otherwise if you're working with an innumerable collection like a a cue the hash table the sorted list you can't do a for loop because you don't have a counter to go across that collection you're going to want to do a for each loop because you can't use a counter to interact with that um actually right there's um [Music] right isn't there a position [Music] right there's an at not remove that right i don't think get by index there you go and you can reference you could do that but why just do a for each at that point um you're doing the exact same thing now you're you're actually allocating a number so you can go and do that for loop and and then allocating memory for that object that that index that you're fetching just do it as a for each so i would only do a for loop with an indexer with a counter um for an array that's the only time i would do a for loop our shia's dog walks on their keyboard and learned how to turn on the laptop that's impressive my my cat uh used to lay on my laptop when i have my laptop on my desk all the time and uh cat was using that to to keep its butt warm and and made it kind of gross when it come back from lunch and the cat was sitting on the laptop know what i'm saying all right moving on how you doing there stealthy archer um when you add four each when you access yes yep um how you doing there uh on youtube uh how it's going it's it's yup mo hello what's up how am i doing i'm doing just fine thank you for asking so uh we talked about the keys there so let's talk about a cube i need to pick up speed here a little bit we've got about a half hour left so here a q is a collection of objects stored and accessed in first in first out order just like just like the line at the coffee shop so you use the enqueue method to add elements to the the queue you add use the dq method to remove elements from the queue you can also peek to inspect the oldest element in the queue all right so let's set up a queue my queue equals q and we're gonna add first second and we'll add third and show me the contents of the queue and it returns first second third in that order because that's the order that they were added to the queue and i can use a count property to see how many elements are in the queue there's three elements so far looks a lot like how we were working with our our hash table or or sorted list i'm adding and removing elements different keyword to work with it and i can use the peak command to just take a peek just show me what the first element is in the queue it doesn't actually remove it but it shows you what it is right there might be decisions you want to make on what's the next element in the queue so we can use the peak method to interact with that now if i want to get an element off the queue dequeue it so dq and it's so it's going to take an element off the queue show me what it is and it's the word first because we had first second third so it pulled first now if i go back and peak again second because my q now right it removed the first element that was in the queue right the first element added to the queue is the first one out just like at the coffee counter first person up up at the beginning of the queue at the at the coffee shop is who gets to place their order yes i'd like to i like a cinnamon dolce latte uh extra whip uh extra shot of uh uh espresso and i'll see you over at the counter and i step away i've been dq'd i'm not in the queue anymore now skippy behind me is the first element in the queue so we can model that same interaction with the queue all right and we're going to talk about stack next let me take a look at chat let me get caught up here you use a for loop to loop through the list of selenium td tags there you go that's a way to do it um how you doing there frumius welcome in good to see you on twitch polywrath i'm so sorry to hear that i hope you have a good time i see the tears but pulse zero says the add method will throw on duplicate key yes index set operator will not right so index set will allow you to randomly interact with elements inside your collection yes um there is also some kind of priority queue in c-sharp uh this isn't an airline priority queue uh no um yeah it's like an update uh yeah yeah it exactly uh is that zyul hassan on youtube yeah um you watched my blazer for beginner series video oh thank you so much i'm i'm glad you enjoyed that um i'm sorry i i don't know how to read the characters my apologies but i very much appreciate the kind words on the blazer video series um we're planning a few more videos to add to that series as we get ready for the.net 6 launch in november and you'll see a lot of me at dot net con for getting things all set up um it's going to be a tremendous event for netconf we're we're doing our final work on uh content beginning of this week and we should have sessions announced by the end of the week uh we're very excited it's going to be a great event for netconf at the beginning of november you'll be able to watch netconf live right here whether you're on youtube you're on learn tv you're on twitch we will be broadcasting to all of them and you'll be able to tune in to dotnetconf on your favorite streaming service watch it on your phone watch it on your tablet on your watch i don't know is that a dick tracy thing you can do now i've never watched a video on my watch but if you can do that you can do that we'll be broadcasting on all three of those services and you should be able to find us everywhere um i ordered my coffee using the order async method now you step away in a wait um folks are queuing up for their lattes there on twitch nice uh patrick fowler says let's go state love the attire that's right let's go my nittany alliance thank you so much um coffee of type t we're gonna get there in just a second that's coming up next a stack type is a collection i mentioned this where it's last in first out so you push and pop javascript folks are familiar with the push and pop methods we use those on a stack to add and remove items you push an item onto the stack you pop an item off of the stack and once again there is a peak method available so that you can examine the next item that could be removed from the stack it's not going to remove it but it just peaks it just shows you hey here's what's next so think of the stack like a deck of cards and that card that has been last placed on top of the deck is the first card that's going to be removed so let's create a stack for my hand here and i'm going to put the ace of diamonds the ace of spades nine of hearts nine of spades nine of clubs i'm gonna put that in my that's a full house that's a full house um that's also mikey's hand from the end of rounders so um if uh if uh teddy kgb is out there watching this is what you're lost to anyways so and then i can display my hand the ace of diamonds is put in first but because nine of clubs was put in last that's the first element to be returned from the stack okay so the the card game references here are going to be rich okay so um if we peak now it shows that nine of clubs is the first element to come off the stack right because it's it's a stack of cards it's the first element to come off of it i really should have a deck of cards here and the only cards that i have here are magic the gathering cards i don't have a deck of playing cards i do have a deck of windows 7 cards here somewhere and i don't see them on my bookshelf no matter um all right so and i can also do that for each a stack is an enumerable type so i can do that same four each and display right this is effectively doing a peek across them so four each and it's doing last in first out so nine of clubs comes first then nine of spades nine of hearts if i display my hand dot peak i get the nine of clubs and when i pop the element off i get the nine of clubs if i run that again i've modified my hand now i've taken the nine of clubs off of that if i run this again now the nine of spades is the first because that was the last item in that was added to the stack and when i peek it's the nine of spades and i'll remove the nine of spades i run this again and it'll start with the nine of hearts etc etc okay so we're interacting with that stack of cards so those are those three basic interactions with those collection types keys values it's a stack that we can interact with and you typically don't really care about the types when you're in using these but what if you do care about the types what if you do want to interact with those types and and and it's important that you store only the same type in your stack your queue your collection so we can do that using generics so let's create a class of type card here oh before i go too much further let me take a look uh at chat yes i see that folks referencing the generic syntax already like a stack of plates at an all you can eat buffet that's another good example yep um the poker example thank you curious drive you like that um dj squared is is referencing um salt and pepper over there a royal flush example no gotta go with rounders round the movie rounders with uh matt damon and um i'm blanking on his name playing teddy kgb uh john malkovich so that kid's got alligator blood that's right uh bryan ski that's right canon aid with the magic the gathering um i uh can i show can i show do i have get over here i got my you're not gonna be able to see it it's too far away i i got um some of the the new set that was just released pre-release was this past weekend you see it doesn't even have a back it's a two-sided card this is arlen one of the new cards that was just released this week if you play magic the gathering um [Music] so queuing three fire yes what's my favorite color to play uh i was very much into white i think i'm gonna be red either red black or red green with with this uh with this uh uh meta coming up olympus tech oh no need to apologize you're watching on youtube you can rewind and scrub and watch it double speed and hear me with a really cool voice if you'd like to um we talked more about magic the gathering later i got stomped this weekend in a sealed deck tournament for the preview but that's a story for another time um so here i can declare for my stack i can declare a class of type card that has ranks in suits and i can put a constructor here for my card so i split those ids so i have the rank in the suit i have a little bit of notation and i'll put a two string on here that formats rank dash suit so i i have some notation for what the card is just like i had a little bit earlier so let's have a deck of cards right it's a new stack because right as we put cards on that stack the the top most the last card that i put on is going to be the first card removed from the deck so we'll create a new stack and we'll start putting cards on the stack now we're putting these cards on just a plain stack here and we're then able to add a joker card later and add jack of hearts so if we then take a look at our stack check this out i've added look look joker is a different type it's not a card joker's just a string here it's a wild card but it's not a card inside my my deck here right i've got cards for these other three items and it knows what the rank and suit are this is a string that's just the value joker for the purposes of working with a deck of cards well that's like having a regular standard deck of 52 cards and and suddenly suddenly somebody drops a magic the gathering card in it or a pokemon card and as you're playing poker all of a sudden all of a sudden you've got ace ace nine nine and pikachu what do you do with pikachu uh pikachu i've got two pair i want my full house but i've got pikachu pikachu doesn't belong there i don't know what to do with pikachu it's mixed things up here hmm we can and and this is the the default set of collections that are available i'm going to take a card off the top of the deck i get a jack of hearts take the next one and i get joker okay that's a wild card i get a king of hearts i get an ace of diamonds and now the stack is empty and i get an error that so here's where you may want to use peak or you may want to use length to ensure you don't go off the end of the stack okay so uh that's why we want to start using things like generics let me take a look at chat squirrel voice mod for free no wonder if this stream will get tagged mtg on clip talk no uh the jeff of hearts that's right it's the jeff of hearts thank you arshia yes it's the jeff of hearts uh thank you for having closed captions you're very welcome i always try to have closed captions enabled uh everywhere so that it's clear for folks who want to participate um so we had a deck of cards but somehow that joker got in there right or or in the other example i was describing you got a pikachu in there that's not a that's not a card that fits in the deck of cards that we expect we expect our deck of cards to always have a standard playing card it's one of the 13 ranks it's one of the four suits joker is something else pikachu is a different type of card it's not a playing card generic is a way for us to declare the collection will always have the same type these other collections that we've been looking at we can put whatever object type we want numbers strings dates you saw me mix and match them and just like we did with that previous example with adding a joker in with the rest of our playing cards if we want to force that deck of cards to always contain a standard playing card we can declare our stack with a type using angle brackets next to it and this will set that type kind of lock it in and give us a little bit of type protection around our collection to ensure that you can only add and access and remove objects of that type from the collection so some of those collections that we saw previously in the notebook here those are available as generic types but the common ones you're going to see folks use our list of tea and dictionary of t right there okay let's take a look at a few examples these are easy ways for you to work with those collections so if we have a list of type t right so we declare this and we can randomly add remove and interact with items so if we have a list of cards we'll declare a new list of type card you see how it does the angle brackets and specifies the type inside of there we declared our card class earlier so we have the card here and we can now add those new cards to this and we can display well show me that type and you see look at this this is a system collections generic list of type submission 60 that's from up above uh but it's a of type card and if i scroll down there's my cards notice unlike when we looked at the the types earlier when we saw it display it's showing me it's not including the type in the output here because we know what the type is the type is always uh right here card it's so it's just emitted here's what these things are so we can still randomly access using that index or we can still randomly access in a list anything in there using that so give me the third element there it is it's the nine of clubs so uh we use these the two index so i got the nine of clubs okay now we can insert to add a card into the middle couldn't do this with uh with the arrays and some of the other collection types we can insert into the middle of a list at a specific index so let's insert the three of hearts in at the third position and there it is so we inserted it at index two which is the third position there's the three of hearts and the nine of clubs gets pushed down so it reallocates memory moves things around puts it back in there and destroys the old copy of things okay just like when we re resized the array let me pause for a second take a look at chat i've got about 15 minutes to go it's like someone shuffling it in that's right yep uh we'll take a look at dictionary in just a little bit oh i see the clip we'll take a look at the clip uh offline thank you so much 3d polywrath we can use index of to locate that object inside the collection inside the list collection there you go you can also ask the list of type card where where it is by using the element at and say well go get me the element at this index so at index one is the the jeff of diamonds um i like that i got to stick with that that was the joke i told last year gosh there um so the list is generically typed to card and we read the list of type card in in english as list of type card that's how we read this type right list of type card so we can't add that joker right doesn't work can i convert from string where'd it go can i convert from string to card can't do it so we get that protection you can even see the red underline here can't do it uh cannot convert right so we get that protection even before you compile it it doesn't try to do that interaction so another good thing around the generics that you're interacting with so there was a question there from dj squared about dictionaries so a dictionary is a key value pair stored a bit like a list here and we define our key as one type our value as a different type and we specify a comma between them so we can now say that remember the file extensions sample we saw earlier we can say dictionary of type string of type string and here's my key value pairs that i'm adding and i'm getting that type safety in here there they are but if i try if i try to make this not jpeg if i try to call this instead 3 right i've already got the red underline there can't do it can't convert it from integer to a string it needs to be a string there all right so i get that once again type safety it's helping me write better code and i can reference items in that dictionary not by the not just by the index but also by the key just like we saw earlier so there it is jpeg compressed images i do have the the type safety um i'm sorry the the key unique key constraint so i can't add the same key twice okay and similarly i can't add something that doesn't belong i can't add right look at that i want to add a card no can't do it cannot convert from card to string not available so type safety this is very important right and this helps to guard that to ensure that your collection only contains the types that you want you may want to allow many different types in your collection that's okay use those collection types we saw earlier but when you want it to only contain one type use these generic types before i get to hash set let me take a look at chat i got about 12 minutes to go here um you follow all the jokes and lessons thank you arshia uh typically you use these as enums versus strings yes yes yum yum rocks on youtube points out you could use enumerations for the file extension sample you would want to use um enumerations to ensure that it stays within that known collection of things sure um this is not python this is c sharp this is all c sharp um types are fantastic for javascript yes it's called typescript am i using some kind of plugin in visual studio code to have different sections this is called a net interactive notebook this is a feature that's available to you in visual studio code it's available to you on github if you go to the github repository i think am i signed in over here i might be so if you go to the github repository for this go down into notebooks and you hit the period key on your keyboard or it's not do i need to be on the root it's not doing it why isn't it doing it is it because i'm not signed in it's probably because i'm not signed in um i guess i'm signed in over on firefox and nope i'm not signed in over here either i'm not going to go through the sign and you hit the period key you get this and it's all installed and running for you so um when would you use immutable collections so i'm not going to get into immutable collections too much here but immutable collections are a collection that doesn't change so um if you want to have a collection that cannot be changed you would use an immutable collection is there a course i have i can follow to learn c sharp um you can also go to that's weird uh you can also go to dot.net learn and we've got a bunch of learning material there for you links to courses on linkedin learning uh courses on uh microsoft learn videos like this one um and of course we have live video content all week long at live.net live.dot.net you can find us broadcasting and answering questions all week long um and and helping folks out i've got about nine minutes to go here let's see if we can get through the end of this a hash set is a high performance collection that does not contain duplicate entries so we can create a hash set of type card once again this will index based on the hash of the object stored so let's store a jeff of clubs and ace of clubs and nine of diamonds and we'll create a three of hearts we can add that in and display the collection and jeff of clubs ace of clubs nine of diamonds three of hearts okay so and actually it stored it based on the index um so if we attempt to add a three of hearts a second time this actually doesn't throw an error and says okay i've already got one in there here's and and i'm not going to do anything i'm not going to throw an error i already know what that is you've already got one in the collection that you can work with it doesn't add some sort of indicator that there's now two of them no there's already one end there there it is okay we can also create our own generic classes so there are official there is official documentation on generics that you can click through right here and learn more about how to create your own classes that can be generically um changed around here so i'm going to create a class called fritz set of type t so we're going to allow you to define some sort of a type that you want to put in the fritz set now i'm going to define an inner list here it's normally not publicly scoped but i'm going to make it publicly scoped just so we can take a look at it we can inspect it a little bit here as part of this so in my collection that i'm defining i'm going to allow you to add an item and i'm going to insert it but if there's zero items i'll put it in at index zero if there's more than zero items i'm gonna put it at a random spot in the collection all right just put it somewhere i don't care where it is it's it's the adhd organized collection we're going to just put it randomly wherever you want in there so now if we insert some numbers into that fritz set so we'll add one two three four five after the first item when it adds it'll randomly place those numbers and when we see the collection now it outputs well three four one five two they're randomly placed in that collection if i run it again it's some other order it's randomly allocating them now that that might be pretty good for the start of a deck of cards collection it's like a stack but different because we're going to put our cards in there and shuffle them up in every which order so let's create a fritz set that holds cards and we'll add ace of diamonds nine of diamonds the jeff of hearts three of clubs and two of spades and if we output the information about that deck well look at that right come here scroll down just a smidge come here there it is so three of clubs was put in the collection fourth well it's first then the two of spades that was put in last then the jeff of hearts etc etc all right if i run that again now it's in a different order okay so we have something now that kind of shuffles as we put things into our our collection that's kind of cool all right um so you can also force using a generic type of object and and you lose that compile time constraints so if we create another collection here and we add the card the jeff of hearts and we add a joker well they're now in whatever order here and right we can get even weirder right and let's add pikachu and joker pikachu and then the jeff of hearts right and you see they're randomly in different orders okay um so what type of object are each element in the object set so it's a card and then a string right so you can store those different objects when you say your generic type is of type object right you're you're effectively breaking it out of the ability to interact with and have that type constraint and those those properties you're able to interact with on those i'm going to briefly touch on link here language integrated query i keep calling it language integrated natural query language integrated query it's l i n q um refers to a collection of technologies that allow you to query data so i like to start with link to objects we're going to get it more into link i believe it's next time so if we take a look at that fritz card collection again we add let's load up some more cards into that using my fritz set so i'll execute that and now now i have a deck object that's loaded up with a bunch of different cards now we have some standard query operators we can use to analyze that collection i've got about three minutes to go here i'm going to run through this very quickly so if we take a look at the count there's 13 items in my my deck of cards that i created there and if we say well show me just those where the suit equals diamonds right it's going to count those where suit equals diamonds this isn't from the playlist we jumped off the playlist there we go so it'll sort through and get me that collection where it this test is satisfied so i get there are six cards that are diamonds so you can use the where method on that collection there to say wear suit equals diamonds and it'll give me those items where the suit is diamonds and i can even do a wear and chain on the count method out here right so let's put some carriage returns in there so it's a little bit easier to see i can chain that together so that it'll do that where and then give you the count so where the suit is diamonds and i get this child collection right this subset of diamonds i've got two minutes to go here and there are six elements in there so i can do that where and i can count and add that filter as well and it shows me that diamonds where count where the rank is ace i only have one card in there okay you can use the select method here to project to change the shape of the object you're returning so give me the diamonds and select show me just the rank properties and i get just those strings of the rank properties right so i can say well any we'll say well give me a true or false if this condition is met is the rank queen so i can say well do you have any queens no false okay go fish go draw another card right um do you have a flush are all the cards the same suit so there's an all method you can use that says make sure this is met no false and we can chain these together so there are a couple other things here order by i am out of time friends i need to wrap this up to our friends that are watching on learn tv it's been great having you hope you have a great rest of your day let me head back over here there we go so good to see you thank you so much for tuning in friends i really appreciate you joining over here it's been a blast talking all of the notes all of the documentation all the samples are out there on the github repository um if you're watching the youtube recording there's a link just below me in the description right there that you can grab to go and uh watch this for those of you in the chat rooms watching live there's a link right there if you want to go download the notebooks and give it a shot um all right my apologies i went a little bit long there we will i believe we're covering more on link next time yes link is our topic next time so we didn't get into the last couple bits of the of the notebook we'll cover all of that next time thank you so much for tuning in those of you that are watching on youtube i hope you have a great rest of your day and for those of you that are watching on twitch i'm gonna set up for a raid uh let me see who else is streaming on the big twitch tv network and we'll get you connected to another tech streamer over here you know what um our friends that code it live are streaming right now talking about web development let me get you connected over to them [Music] thank you so much for watching i really appreciate you joining us over here on the visual studio channel when you're done checking out the code it live channel those of you that are watching both on youtube and twitch make sure you come on back here a little bit later in about an hour or two my friends uh from the docs team will be back and they'll be teaching all about and talking about things from the microsoft docs i don't know what's on their schedule for today have a great time i hope you have a great day of learning and i will see you next time take care you
Info
Channel: dotNET
Views: 5,658
Rating: undefined out of 5
Keywords: #csharp, #beginner, #dotnet
Id: CDfI0BldN-A
Channel Id: undefined
Length: 114min 45sec (6885 seconds)
Published: Mon Sep 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.