Learn C# with CSharpFritz - Interfaces and Error Handling

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] [Music] [Music] [Music] [Music] hey we got it right hello good morning good afternoon good evening whatever time it might be where you are out there welcome back to the live stream my name is jeff fritz uh how you doing um we're set for another uh c-sharp with c-sharp fritz episode and i am using the wrong keys there to try and adjust my scene how's things chat room how you doing my goodness it has been it's been a couple days we i feel like we've gotten through a lot here let me look at just over here i'm going to take a look at the chat room they're right right there hello hello let me say hello see and um gosh i i was doing some some work here in the room and i got my cameras adjusted a little bit there there we go chris jones is here moises is here from sao paulo how you doing there d flux is is here uh is that ajiba from ivory coast and pulaski nitro evil good to see you our friends over there on twitch anthony park has dialed in from miami charles galloway is here dimitri from ukraine hello good to see you uh mr joe b is in the netherlands uh got your mini cookies nice nice ready to rock some c-sharp let's do that we're gonna spend some time learning today it's always i i love learning here on twitch and talking about what's happening with our programming languages and frameworks so much fun and and it's even more fun because i get to talk to you i want to hear from you i want you to write in chat here say hello like some of these fine folks and i want to i want to talk to you of course we're going to acknowledge you and say hello there um to folks like uh let's see follow uh carlos diaz from the dominican good to see you nikolai new limb hello hello you just got time for some more c-sharp learning i think soviet doge uh forgot the shift on that c but we we got you there how you doing there jose deflux under a fair amount of stress to get your blazer project out the door good luck to you there mahesh hello hello stoney in ohio hello hassan uh bentley tube in vietnam hello good good evening to you uh guaran is here uh ajay from india hoi good evening to you ahmed sin deep in india as well nick howdy howdy uh farhad in ukraine oh my goodness hassan um carlos alarcon uh in chile nice nice thaddeus in nigeria shai sharp says good afternoon to fritz and friends so good to see you um i'm sorry i don't i don't know how to pronounce the uh cyrillic there but hello to you in ukraine ahmed hello from windows 11 hello to you in windows 11. we won't be using windows today more on that in a little bit um let me see here is that luke velez in colombia how's it going narolo in central asia in tajikistan good to see you uh rav is in london uh another cyrillic name i'm sorry um i don't know how to pronounce those hello to you in russia naveen pti costa rica i've got it i've got somebody whose name is all of costa rica good to see you tg in new jersey how's it going there sabine in india uh muhammad in south africa hello hello friends ah so it's good to see all of you we went through last week we spent time talking about the errata other questions and things that that i kind of skipped over i kind of skipped past in our learning about c sharp this show is always dedicated to beginners you might be an expert out there you might be somebody who's been doing programming with c sharp for 2 5 10 even 20 years but we want to stay focused we want to stay very welcoming for those folks who are beginners we want to encourage them ask questions let's talk i all the samples that i'm going to show you they're available out on github you can download walk through them at your own pace you can rewind and re-watch the recording whenever you'd like what's important here is that we answer your questions we talk to you we i want to hear from you i want to know what kind of challenge you challenges you have around the c-sharp programming language around.net and and how we can make that better or how we can help you out get you a solution to move forward now i'm not going to be able to look at your code here live on stream and say oh yeah here's where your bug is but at least answer some questions around our topic around the programming language um i can't ask answer questions about windows 11 i can't answer too many deep questions about visual studio during this session we go to another sessions where we go are more focused on those topics but for me today we're talking about c sharp we're going to be talking about interfaces abstract classes inheritance we're going to talk about error handling a little bit so that's where we're going to be spending our time i want to make sure that that yeah we get you some feedback if that's something that you've been struggling with you want to work with uh let me look back at chat over here no lean likes the gunners thank you i'm glad you like the gunner glasses i'm a big fan of them um my eye doctor after looking at my eyes on my last appointment said you need to start doing something about the blue light here you you need to work on this bartos in poland good to see you uh ajay says namaste well namaste to you as well uh russian uh is that fazliev hello soviet doge from 127.001 i like what you've done there i like what you've done there lenny the rabbit is from one twenty seven zero zero one as well that's a that's a networking joke uh avtooni from georgia georgia the country not the state all right uh jayakumar from india good evening to you technosoft from egypt am i going into dependency injection as well um that's going to be a couple weeks down next week we're going to try and get into async await and multi-threading talk a little bit about asynchronous operations multi-threaded coding and how how we can get you started with that it's a very complex topic there's a lot of different concerns in there it's going to be a brand new session that i'm writing this week that we'll be talking about you like the github hat thank you so much ladybug in singamore singapore i'm sorry listen to me i can't even speak rick i need some more coffee uh my positivity is infectious how do you do it i got a nice cup of good morning america cup of hot coffee a little bit of pumpkin spice yeah i went pumpkin spice today all right don't knock me on that i went i went with the flavored pumpkin spice coffee all right chat room i know some folks are like ah pumpkin spice but you know what it's october here in the states and gotta enjoy gotta celebrate gotta have a good time um it's new zealand at 2 am for bucky oh my goodness bucky i'm flattered you're staying up so late please i this is being recorded for those of you who don't know over on over on the youtubes the tube of all thing use is that how that works i don't think that's how that works um you'll be able to find the recording of this if if you're staying up so late and and you happen to fall asleep that's okay you'll be able to catch back up but i'm flattered folks are staying up late to tune in uh svetoslav from bulgaria you know what that's the that's the furthest east in furthest east in europe that i've traveled haven't been to asia i haven't been to africa um bulgaria uh i was in czech republic a couple years ago but uh yeah bulgaria is about the furthest east that i've been i think so um good morning uh is that is it amal or ajmal i'm not sure i'd pronounce that uh raul in spain how's it going there uh wanzhou in the canary islands recommend to handle errors in railway-oriented programming style railway orient i haven't heard that before hello to you in serbia vlada good to see you so of course i'm going to have some good music playing here in the background we're going to get in we're going to start talking about the topic i want to make sure that you have links so that you can check it and actually did i i forgot to plug my mouse back in um my wireless mouse that i use from the other set let me get that see there's the other mouse being identified let me get you a link over to the github repository where all things that we'll be discussing today you can get to and you'll be able to uh clone work with locally [Music] there we go there's a link for you out to the github website there it is uh we're gonna be in the notebooks and we're gonna be working on zero one zero eight is the notebook we're gonna be working on today and i'll show you that from visual studio code in just a minute now for uh those of you who don't follow me on on twitch that's okay uh you can find me on twitch i am twitch.tv slash c sharp frets just like my twitter account my github uh my instagram my paypal my uh tick tock yeah tech talk i'm c sharp fritz on tick tock also um but um if you follow me over there you know that i observe every october and this is the third fourth fourth year running that we're observing i think it's fourth third i might be the third it might be that's probably the third it that we're observing uh uh ubuntu yeah i took the i took ubuntu in october and i kind of mashed them together and we have ubentober it's a month dedicated to ubuntu linux on my stream and i figured i'd bring it over here to the visual studio channel into the.net youtube and we talk about workingwith.net on ubuntu linux so we're going to be going through we're going to be using visual studio code today but we're going to be running ubuntu linux directly on the metal no window subsystem for linux involved here no funny virtual machines no we're directly on the metal work and we're going to be using uh net on linux because dot net works everywhere whether you're running on windows mac or linux ios or android you can you can deploy net applications it's great stuff um and we're going to get into it in just a little bit hey arshia good to see you i'm doing well allah ah allah waba from cairo hello welcome in train style is boom there's a big air choo choo no i don't think that's what they mean oh my goodness let me get some music playing in the background here this is stream beats from our friend harris heller this is music that's dmca free royalty free you can play it wherever you'd like instrumental music that you can listen to and that's not quite loud enough there we go just there underneath things you can listen to this wherever you'd like you can find all the music to download at streambeats.com there's also playlists there for apple music amazon music and spotify you can download the music if you'd like as well check it out streambeats.com thank you so much to harris heller for making your music available for us to listen to while we write some code together today is tutor is that is tutor or is it it the door i'm not sure how you pronounce that uh from romania welcome hello to you uh erbert herbert herbert from taiwan i know i'm i'm i'm starting to fail in my name pronunciations here this is a bad thing it's time for my coffee so um let's think what else do we need to do we've we talked about ubuntu said load everybody we're about 20 minutes in here i like to give it just a little bit of time to make sure that that uh we we have uh everybody dialed in that they'd like that would like to be here before we get started um let me see taking a look over here yeah that number's not right i knew that number wasn't right i knew that number wasn't right um yeah hello to you uh olamide in nigeria good to be here good to have you here thank you for joining us uh yum yum rock says rider is a great ide for linux however it is a paid for id um that's okay right look if you find great tools for that help you do your job you should support those developers whether they're open source whether they're closed source if they're paid paid for tools you should support them if you're making a living off of them because you want to make sure that they continue making those tools and let's face it developers need to get paid in order to continue building cool things so i encourage you if you if you find some tools that you like whether it's visual studio whether it's it's some open source tool that has a sponsorship program on github or or a patreon or something like that go out there and support those developers i'm using obs the open broadcaster software to to produce this video right now for you and i'm i'm a subscriber on their patreon you can go out there and you can see that i'm a paid subscriber helping helping those folks because i believe in the software i want it to grow and continue being developed uh let's see hey how's it going there terry having some pecan is is it pecan or pecan pecan pie flavored coffee with creamer in arkansas oh your razorbacks just missed beating ole miss this weekend college football was not kind to my pennsylvania state my penn state nittany lions in iowa city this weekend was not kind to them at all um but all the best to to those iowa hawkeyes um arshia says whatever you use makuu bunter windows you have the same uh the same i'm not sure feature and you have it it's your beard i'm not i'm kind of lost there on that one i'm sorry uh you have the same best features yes absolutely uh russell ryder has some open source licensing program student programs as well yeah they do they do so um so take your pic find the tools that work for you try them out figure out how they fit into your workflow and absolutely support those folks especially if you're making a living off of them right folks that that use um use photoshop on a on a pirated license ah come on you're making a living charging a lot of money and you didn't pay for photoshop come on um so there's there's definitely there's definitely things to be said for for paying for quality tools software frameworks um but with microsoft all the visual studio code open source free anybody can use it all of the.net frameworks free open source anybody can use it you can download and interact with it however you'd like so really cool stuff writer can't debug uwp yeah why don't say more a blazer uh i'm not sure what you're asking there alexander what about blazer uh hey soviet doge yeah look at that ah look at that my um my emotes from my channel over there on twitch have a halloween theme yes my bot is uh dressed up as a skeleton for halloween also there i i'm not sure if i say it too much here on on this stream i say darn skippy a lot instead of right i try and avoid um i try and avoid using any colorful metaphors any swearing so i'll say i'll say darn skippy which of course is a little bit of a reference to a peanut butter here in the states uh we modified the icon that looks like the peanut butter jar and it says more candy so we did something for that with that always pay for devexpress every year you like devexpress awesome and you love their blazer components there's some really great blazer components from uh the the dot net component of vendors whether it's devexpress component one i think they're they're also grapecity there you go there's the more candia modes um uh devexpress uh grapecity uh infogistics progress telerik uh syncfusion they all make really great components they're gonna help you uh do your thing right there um immensin say if i made changes to net runtime socket api would those pr's have any chance of being accepted so that's a that's a great question for anybody who's working in open source and and let's let's talk that through for anybody who's working in open source if you want to submit changes if you want to submit suggestions for a fix an improvement like like amensin's suggesting here um what you do is most folks say hey please open an issue first let's discuss the type of change you're making because we want to make sure that that um we want to make sure that you're doing something that we all kind of agree with otherwise they're not going to accept it they're not even they're just going to straight deny and say well i don't know why you made this thing we didn't ask for it we don't understand why it's being submitted and most importantly when you submit something back to an open source project whether it's dotnet or or any other project out there the folks who are maintaining that project now need to maintain the code you wrote so they want to make sure they understand it and understand why it's being created before they accept something being submitted um let me see there so a couple of mentions from stream elements um nitro evil says i noticed the asp template for web api has removed startup css and is merged to program csn.net 6. that's correct um in netsex in the um and i'll get to lolo's comment there in a second in.net sex you're going to see if you're familiar with net5 and the.net core project templates before that you're going to find that startup sequences and the templates are much much more simplified so that there's one file needed to kick off an application whether it's a windows forms application or a wpf application an asp.net core uh web application you're going to find one file that kind of bootstraps and starts the application this way we get out of that shim of things that you need to do to set up your application and get right into the business process the business logic for your application right away in in some of that boilerplate code that gets things running um it gets pushed out of the way it gets done and it is simplified as much as possible lolo has a question here what i really miss is an on error resume next well that was very much a visual basic thing um ignore all errors and proceed to the next line of code maybe with a block of with errors ignore um yeah i was a developer back in the day when we had that uh happening and honestly i like to try catch block because it gives me a way to isolate exactly here only try only interact with this block of code so very easy for me to see here's the only things that we're looking for an error in that we're expecting to interact with rambling geek says c-sharp is moving so fast uh there's a new release of c-sharp every year um just like there's new releases of other programming languages uh on a frequent basis so the the fact that you have a dedicated c-sharp development team building the language investing testing getting feedback as they're evolving the language i think that's a pretty cool thing zombies when for when they first saw the changes in dot net 6 and how the code is templated a little worried and confused uh just so used to the older ones yeah you know what zombies i was the same way it gosh where did all the things go here um but the the good thing to know is you can still write software using the old older templated way no reason for you to ditch rewrite some of that code it's okay it works just fine like in if you want to build in work the previous way yum yum rocks as minimal apis are interesting hopefully they get more code out for helping with unit testing before the release um we're about a month and a half not even we're about a month away we are one month from.net conf our big um how do i how do i how do i describe dot net conf our big annual uh virtual conference our 11th year doing this and our launch for net six is about a month away um it's actually it's a month from tomorrow that uh i'll be in studio i'll be i'm gonna fly out to seattle and we're gonna we're gonna have the big.net conf launch it's gonna be uh it's gonna be a great event we're really looking forward to it uh space shots as we all get used to the boilerplate template no matter the framework yes we do sometimes we forget how the standard startup code can make it harder for others to join us in our favorite frameworks and platforms yes that's something that was actually quite intimidating for new folks trying to get into c-sharp and get into dot-net by simplifying things we make it easier to get in and just start building an application start building and working with forms why do i have to learn what public static void main is and this whole startup thing put it in one file and get folks into building and working with it as quickly as possible you want to see what we're going to see in november you feel it's huge um so with net being open source everything's been kind of documented and out there folks see and know what's happening what's what's coming with it so we're we're in pretty good shape getting ready for that all right um let's see let's do this i'm gonna kick off a refresher on my chat app over there because it is not up to date there we go um c sharp is moving slow so okay just a few minutes a few lines up we had somebody say it's moving too fast no higher kinded types what do you mean no built-in aspect-oriented features um what programming language do have aspect-oriented features and those are things that the team is purposely left for the community to build so higher yeah what do you mean by higher kinded types because there's a point where the programming language stops the frameworks pick up and move on um is there some massive project inside microsoft that drives language features sometimes i get hardcore stuck then suddenly there's a crazy new c-sharp feature that solves the problem um so the c-sharp team is they're very focused right they're first-party customers are folks like like bing the search engine uh folks that are building azure folks that are building and working on on parts of windows um so there's there's a bit to be said there about those are their first party uh customers not to mention the dot-net framework and when we want to do things like dramatically minimize the startup configuration for a net application that requires some c-sharp language features right implicit usings global usings those were things that needed to be defined and added to the programming language in order for them to become available for for the templates and things so very much the the programming language is looking at and the language designers look at what's happening in other languages other metaphors other programming styles and things that folks take advantage of and enjoy and determine do we need a similar feature in c sharp they test it they evaluate it and and they circulate uh samples for folks to interact with before they decide okay we're going to move forward and start adding this to the language so um yeah they've been trying to figure out how to broaden the abilities without fundamentally changing it yep that's right 3d polywrath loves xamarin but a bit of their bar it is a bit of a barrier to new developers yes so simplifying that right if we think about net maui now with a much simpler startup experience you're going to be able to jump in and start building.net maui applications much much faster what's the twitch link on twitch we are twitch tv today we are twitch tv visual studio um i'm twitch tv c-sharp fritz if you want to see me um what's really cool seeing the speed improvements with bing yes uh you use the articles microsoft publisher published on the improvements to get time allocated to move production apps to dotnet 5 from net framework very cool yup yum rocks um you're going to see more about how uh the big performance improvements in.net 5 and now.net 6 improve and make uh make microsoft apps better it's it's coming there's lots more to be talked about there because uh very much the net team is listening to other microsoft applications and and how those folks build and work with net how focused is the dot-net team on providing zero allocation framework features using things such as the new value task very focused because driving down memory allocation makes things a lot faster do they make an effort in promoting such practices for third-party developers sure do however to be clear dot-net developers like to prematurely optimize things building things that you don't need first and think well because i optimized and i'm only using value types it's going to be running much faster if you're if you still have bad practices elsewhere that are easier to pick up and detect than how you're juggling memory that's going to be a much more significant um issue for you to address any good sources docs for building models with asp um somebody else might be able to help you with that nitro evil i don't building models i'm not sure what you mean it's always a little weird for me as asp.net dev and fan of the framework how huge swaths of the industry still don't know that it is truly cross platform says carl codes agreed the number of and that's why i am spending today and all month on stream programming with ubuntu linux because there are a lot of folks out there who think that that dot net visual studio are just a windows thing you can't do gosh i can't be serious on my mac i can't do this on on my linux box no you can't and it's it's really good um dot net pro approach of doing everything out of the box annoys you says jens uh sometimes you spend more time researching what it can do for you uh so here's the thing right what jens was saying there's very much two groups that that are diametrically opposite that that are debating this um quite quite hotly there there's there's a group that says i don't want you doing anything for me i want to build it all myself and there's another group that that says just like we saw in the comments earlier why doesn't c sharp why doesn't.net include all of these things in the box it needs to be have all of these other features well which one is it do you want the the template that is pre-configured and has all the things or do you want the template that starts with nothing and you have to build it yourself the answer is you can do both you can do both you're not forced to do either but it's there for you if you want to use all the pre-configured things you don't like maui you like uno platform because you can use your favorite uwp api and win ui um that's fine it's up to you um we think maui is going to be a tremendous platform for everybody to use what do i think about the future of blazer um blazer is gonna be around for a very long time it's shipping as part of net six so you're gonna get at least as with all of our long-term support releases you're gonna get at least uh four more years of support on that for folks to be able to work with that why am i streaming under science and technology and why not so and not under software and game development um i don't know does anyone like nullable types yeah it's all right it helps folks um avoid getting involved with uh dealing with nulls hoy asks what do i think about.net development on apple silicon um i think apple pulled the trigger on their silicon um without getting anybody involved and ready to go [Music] to work with it there's a lot of folks that work with third-party technologies that are really struggling with getting it working on apple silicon you you can do it it's not as fast as as apple claims it could be um and we're working on that but because apple just launched it and didn't have any kind of folks uh being able to tune and interact and work with their engineers before the launch we're struggling to to catch up and get supported over there so good afternoon to you in norway uh hi shono is that you pronounce it nordic lights uh build yourself as your go-to it's up to you you can do that simplified documents about windows forms um go to dot.net and click learn there that's the best i can offer you you want to learn mo mobile programming going to start with net maui do you need to know xamarin beforehand say asks hamed no bartas asking the question we get every stream and i'm gonna answer it the same way every stream why not maui for linux maui supports four different platforms windows mac android ios and there is a community group that has started working on a linux uh runtime for it the team's already running behind trying to get those first four platforms done the number of linux users is orders of magnitude less than windows ios android and mac os so it wasn't a priority for the first cut because they already knew they had a mammoth job to accomplish but there is a community group working on a runtime for maui you're welcome to jump in there and help out um i would trust code used by millions more than something homegrown i completely agree yum yum rocks um so um are we gonna see microsoft cars too um we made microsoft cars a couple years ago prototypes that were shown off at build and ignite there was a truck there was a mustang um it was a truck no it was a jeep i think um really cool stuff and they didn't go too much further with it uh tmk dk says apple is a lock system yes it is hey frackberg good to see you code with sean good to see you let me get into this let me head over let's start talking about our project today we're about 40 minutes on we've gone through and had a nice conversation here and let's head over and uh get started with um [Music] with our notebooks and getting into the project today is there a design editor for maui um i thought there was a designer that they were working on i thought that they were bringing the xaml designer in there and and you were able to use it um i don't work with maui too much but we would have to take a look it would be in visual studio 2022 that you would find it but i'm i'm not sure um i would have to defer on that until i could take a look but today we're going to be over on ubuntu linux hey james hello let me uh let me head over to the other set and let's get started um uh oh am i on this headset there we go we're on the other headset uh let's see do i have all the things let me grab the coffee grab the phone and uh yeah let's head over to the other set and talk about interfaces abstract classes and uh error handling ooh i better move this up so i can see that up here there we go all right see in a minute over at the other set heading over doing the thing and there it is right there because this machine went to sleep on me we'll log back in there we go there's our session information for today um wow closures leak memory then we get the foo static user state if foo accepts action uh this can be syntactic candy sure you could do that looks like it uh they are implementing a preview previewer for maui yes let me bump up the font size on that real quick just use a smidge there there we go that's better ah there we go all right when should i use try catch i often have a lot of functions and all of them have exceptions well you got a problem you got a problem um like we'll we'll talk about that in a little bit we're going to get into uh functions in uh i'm sorry try catch error handling here in just a little bit this is um this is notebook zero one zero eight you can see it right there interfaces and error handling um and we're gonna talk about abstraction we're gonna talk about building um abstract classes and interfaces and how we can work with them inside of our code we're going to get back to try catch a little bit longer a little bit further along here so we've previously talked about the protected keyword we've seen some interfaces in some other sessions we talked about i innumerable the innumerable interface but we haven't actually gotten into the science of object-oriented programming and how these pieces fit together right how how where i enumerable happens how we use that and how we can make more of our objects in c-sharp so how do we do that what do we do with these things so there's there's several things that we're going to talk about we're going to get into the first of them is abstract classes it's way over there there it is now see i i really need to put that display over here so i can see what's going on um everything in c sharp is an object and you can derive one class from another and this is the is a relationship between two classes so i can define an abstract class here called shape right i could say well we're going to define what a shape is and right now shape doesn't have any properties it's just an abstract class and it has that that abstract keyword right there okay so what that says is you can't create a shape class directly you can't say new shape that doesn't work but you can say this class is a shape like i have here with triangle rectangle and square so i have class triangle you see colon shape right triangle is a shape rectangle is a shape but i can also inherit from other classes that aren't necessarily abstract so i can say class square is a rectangle and this is this initial piece of inheritance that i want to talk about these are inheriting features from the classes that they are defined to be related to okay so my square is a rectangle and a rectangle is a shape so this way i also know then transitively that a square is a shape makes sense very english way to describe that but i i think that relationship between these classes becomes clearer when you start thinking about the using the is uh text uh description um let's see yes what is the is a relationship right yes like a parent right so we we define um right where we're defining that this class inherits from it is a right it is one of these right a square is a rectangle that's what i mean by the is a relationship right when you when you put together the sentence it starts to make sense that way um yes there is a a github made available and i've i shared it a little bit earlier um is it going to give me the ability to post that message again um i had the github up here somewhere i don't have it handy can somebody uh share a link to the github so yes these are net interactive notebooks multiple inheritance isn't a thing in c sharp you can only inherit from one class so we say we uh right and that's where things here right allow us to inherit right from this entire stack right square is a rectangle is a shape so we have this stack of inheritance these ancestors of a square that are initially defined here so these are net interactive notebooks these do they do work um using jupiter the jupiter notebook technology so yes triangle is a shape and we're going to see that relationship used a little bit later thank you um thank you space shot for sharing the link um this isn't every day i'm only on mondays i'm here live um you can find me tuesdays and thursdays on twitch talking about blazer talking about azure over on those channels so uh edmondson talk it with a very good question let me come to that in just a second i want to say hello to ivan good to see you so this is inheritance we're inheriting the behaviors and properties of these other classes when you when you build an object and you build the things that you're working with right you're inheriting features from this class to the classes above it you can also build your classes build your objects by composing features by inserting into that class specific classes objects that do those specific things that they need so a very common example is something called the repository pattern where you have a repository some class that knows how to work with your data some data store that it's working with so we call it a repository and that repository class will have uh create read update and delete methods and it knows how to go and work with a database or your file system or an api somewhere out there to work with but that repository to your other classes knows how to do those things you would compose a class that works with that by inserting that repository into the class to work with instead of having some sort of a class that inherits and has a property has those features that knows how to go and interact with the repository i'm a strong believer in composing your objects because it gives you a lot more flexibility to inject unit tests i'm sorry inject fakes for unit tests later and we'll see that in a unit testing session a couple weeks down the line here so we'll talk more about composing in just a little bit where do i get my caps well this one i got this is the github hat i'm wearing today i got from the github swag shop i don't think they make this hat anymore i think they sold out of all of them um richard how you doing there friend um alrighty is there any abstract clay are there any abstract classes in clip talk oh yes very many um if you watch the stream that i do on tuesdays and thursdays i've been building a uh i've been building a website with blazer and azure functions called clip talk that website is built with blazer that runs in the browser and there are many abstract classes running around there particularly around how i work with data repositories i have base repository objects that know how to interact with that know how to work with whatever database system that i'm using and i inherit from that and shape that repository appropriately for the various entities that i need to work with so in in the case of clip talk i work with video clips so i have a repository object that knows how to interact with video clips consequently then when it's worked when i write that class to work with video clips i'm able to point back to all the features of that database that are in the parent class to work with we'll see how you can do that kind of thing here in just a minute um is controller a repository pattern class no it is not yen's no controller is different um would it be bad to do data access within an abstraction class service layer rather than a repository different concept evan i'm probably went a little bit further there dependency injection needs abstract classes no dependency injection can also work with interfaces we'll see that in a minute here let me let me continue down here i went a little bit further ahead to answer a question and i brought up that repository example let me show you a little bit more here and and it might clear things up a bit so we've def we've defined this abstract this class as abstract using the abstract keyword here all right so i will x uh yes.net interactive there it goes executed and there's nothing to report because it doesn't actually do anything yet but we can add methods we can add properties to our abstract class like this just like we would have inside of any other class we've defined that our class shape has a parameter and and we'll just by default return 10 here and we're also going to define a method called area and we'll return 15. i don't know what it is yet we'll just make it really simple and we're going to force those values in there so that we have these methods that we can interact with so i still have my triangle my rectangle and square still defined the same way but now i can execute the perimeter and area methods on triangle rectangle and square if i execute that and i say give me a new triangle and ask for its area well i get 15. i don't really know if that's the area of the triangle but i can also right do this for a rectangle 15 right um and if we want to do that for a square 15 right and it they also have right the perimeter method right there it is perimeter and 10. okay so it knows how to do that interaction it knows that all of those objects have these methods but it's probably not what we want something hard coded on there we should probably have those and interact with whatever that class type is and pass some information back and forth so that we know a shape has perimeter and area and we actually want to do some calculations that are specific to each one of those right we know a triangle's area is a half base times the height of the triangle right and a square's area is is the side squared right and a rectangle and so on and so forth so we should take those into account as we build our objects so that we're sure that shapes have those methods and apply the appropriate formulas for those shapes in each class we'll see that in a second let me take a look over here at chat um catching up here um uh mez youssef asks what's my laptop brand this is a kubuntu focus this is a k-focus um this is not an m2 but uh this is they've released a couple new versions of their laptop but this one um this is shipped with an i7 10000 series uh 64 gig of ram it has an nvidia this one has a 2800 uh nvidia 2800 card in it you can see the nvidia logo right there um and uh beautiful display for me so i really enjoy using this big thanks to the folks at uh at kubuntu focus for letting me use their laptop here on stream for um for ubentober uh no it's not a framework laptop nope nope nope abstract class indicates it's a class you never intend to make an instance of i'm not sure the intent is there but it's definitely a constraint you're not you can't make an instance of an abstract class uh yvonne says i should go come to serbia um when we get on the other side of of pandemic and i'm i'm eligible to travel to europe maybe we can talk about it then um yes basics uh implementations in abstract classes that's right effectively creating blueprints russell yes we're gonna get there in just a second uh sheepy uh says so square asks so square triangle and rectangle all inherit from shape yes and those are the classes that i create but the shape class itself you can never directly create that's correct so if i were to change this and say well give me a new shape you cannot create an instance of the extra abstract type you can see it or interface shape okay can't do that but you can certainly make a square shape is just defining that hey there's this thing out here this general concept of shape and right i i like the term that that was mentioned earlier it it creates those blueprints that these other things know how to implement and we can work with so yes i'm using my raspberry pi mouse i do have a raspberry pi uh the keyboard we could do a raspberry pi episode here i guess but i do have i do have all of that that might be fun to do next time do everything on a raspberry pi i'm going to write that down i'm going to make sure that that we talk about that next time um difference between interface and abstract class spoiler alert it's a little bit further down in the notebook here um why do blazer wasm downloads around nine meg each time that's not very production ready um your when in development mode it does when you publish it tree shakes and we've gotten that down under two meg you want to take a look at a blazer web assembly uh site that has a much smaller footprint check out hats.csharpfritz.com in fact i'll even bring i'll even bring that one up real quick here um so and i'll do it with firefox hats c sharp fritz com this is a blazer website that i wrote to show off my hat collection and there it is there's all my hats you can take a look in and tinker around here if you want to look at a very simple blazer website that shows off all the goofy hats that i have and that i wear live on video and the source code is available as well this is an azure static website it it delivers a much smaller footprint than nine meg of data so um all right back over here um 2meg is still a lot 2meg is actually smaller than many websites including the website you're watching this on whether it's youtube or twitch the amount of code that they deliver into your browser is a lot bigger than 2meg so all right um so right here i go saying parameter and area are calculated a little bit differently so if we want to define and say these methods are available and you must implement it in the the classes that inherit from it we can mark that method as abstract okay so right there you see public abstract perimeter abstract decimal area so we're defining these are methods that are available in all shape classes and since they're defined that way they must also be defined inside of triangle rectangle and square okay so if we take a look at triangle perimeter is side one side two side three area i put together and i'm using heron's formula here is a half times the perimeter and we take the square root of the semi parameter and uh subtract each the the product of each of the differences of the lengths and that's how we get the area every time right we could do the base times the height and all that stuff but you have the sides as properties here and it makes it real easy then to derive the area when we have a rectangle we have a length in width and the perimeter is length times two width times two area length times width and a square is just a type of rectangle right so now i can do these same things here i can define a triangle with its three sides lengths a three four five that's a right triangle and i can ask for and say display the area and display the perimeter let me uh let's do this let's clear all the outputs run that again so right if it's a three four five the perimeter is uh is 12 and the area is 6. so um base times height 3 times 4 is 12 half of that is 6 because it's a right triangle real easy to figure that out so my triangle has the same area and perimeter methods right i can do the same thing with a rectangle right right i could say varekht equals new rectangle right and i can say sure let's say length equals three width equals four thank you uh thank you github copilot um and i can say wrecked area sure and gives me the rectangles area is 12 right so 3 times 4 is 12. i can also ask for the perimeter because i know it's a shape and those have those methods then i get the perimeter 14 right 3 plus 4 is 7 doubled 14 there we go so right we've we've defined that a shape always has these methods and we're able to always call them even in those classes that inherit from shape we've provided the appropriate implementation the formulas for those shapes in those specific classes so that they adhere and have those same capabilities as all shapes let me take a look at chat and get caught up here make sure that we're all in good shape i didn't mean that um what about a constructor in abstract classes we can do that also didn't know i run the visual studio channel oh yeah absolutely uh we can talk about i think i get into constructor here in just a little bit uh i'll have to scroll down but if i don't we'll we'll touch base on that is there a way to inherit from multiple classes ask mighty cell no c sharp only has a single class inheritance you cannot inherit from this class and that class in that class one class at a time um [Music] so how is clip talk going it's going very well i was migrating i had a million clips i had to migrate to our new database last night and it took the better part of an hour to do that migration and then another hour for all the indexes to catch up um got a little bit more work that i'm going to push through later today of just letting things migrate letting it sit and the indexes adjust and we'll see how it how it's running i should be able to turn that on tonight how to serialize drill deserialize with json that's a bit off topic look at system text json.json serializer there are a couple of methods on the json serializer class in the system text json namespace that you can work with that will do that very very quickly very memory efficient in.net 31.net core31.net5.net 6. hello to you uh co co-lojo uh barayan in kenya good to see you hey bananas good good going yes we are in good shape is clip talk now on ravendb almost i didn't turn it on only because i wanted the indexes to shake out last night but uh there is now 5.5 million clips running on the staging servers for clip talk um multiple interfaces different from in from implementing from inheriting from multiple classes ah kyong awesome question kyong i love that question thank you thank you for asking that question that's a great question what's the difference between shape of t equals new triangle versus triangle of t is new triangle kyung noticed that i use the var keyword here great catch thank you so much look at that check out the big brain on kyung awesome stuff check this out so i'm using the var keyword here to say c sharp when you compile this in fact go figure out whatever the type is here on the right side in this case triangle make make that variable make t one of those you can do that however what some folks like to do is and there's nothing wrong with this is they like to be very explicit in what type that they're declaring their variables for so as as pointed out by kyung i can say triangle i can force this to say this is a triangle there is no performance difference in your running code whether you say var or triangle okay that is only a thing that happens at compile time where it just goes and figures out the var it might take like a couple milliseconds to go figure out and set that type appropriately for you so the question is i said triangle of t but we know that triangle is also a shape what happens if we say shape there what happens if we say fritz needs to get the capitalization correct there on the shape class you can do this also and it still works but if i uh let's comment out this block down here just because i don't if i say display t right it knows that this is a triangle and it's displaying there's the three sides right those are the three properties of it however if i say get type of t it says it's a triangle however if and nuts i scrolled and it and i didn't mean to however if i go to work with this it doesn't have those sides available to me if i say display t side one length error shape doesn't contain that property what this is done is i've pushed it into a less specific type a shape not a triangle and i'm able to work with only those things that are specific to a shape in my code now i can force it back to a triangle because it is a triangle right so i can say if i put in parentheses here i can i can cast it back into that other type by saying helps if i get that triangle yeah uh t let's put some more parentheses around this so we get this right right i'm a big fan of using more parentheses and now i can say side one length um and i'm missing i'm missing a right that one uh oh i've got too many there we go now i can find it because i've cast it now i can cast i can change the type of something by using parentheses here to change the type of that variable and start interacting with it so because it was defined as a shape but it is actually a triangle that's stored in there it's still a triangle stored in memory but for the purposes of working with the rest of the code i'm only allowed to interact with the shape properties methods and events i'm able to cast it back we'll start thinking of this as a triangle by putting triangle in parentheses here to cast to change its type and work with it really good question there thank you for the question kyung really appreciate you bringing that up that you can do both and it might be okay to do both because you may want to be able to generically refer to whatever this object is maybe it's a property on a class and you may pass in a triangle you may pass in a square and interact with it differently well interact with it the same way inside of your class even though it behaves differently inside of the triangle and square classes let me head back up here take a look at chat um you can't do that in golang um you know what i'm not familiar with go i'm sorry um somebody asked can i do c plus plus no i'm sorry this is you're talking to a guy named c sharp fritz hey i don't teach c plus here i'm sorry um you can't access triangle properties if you declare it as a shape without casting that's right um in a large codebase is it quicker and better to use explicit typecasting no um no it makes no difference if you override them for every shape it can't change side lengths right in the voi i think that's right i'm a little confused um [Music] what's the difference between is and casting um so we can also because we we know that t right we can display um right run that i can say display t and there's a a keyword here called is that you can use and i could say well display t is triangle and it does a test is this object can it be cast to that type so t is triangle true t is shape true t is a rectangle false t is not a rectangle t is a triangle that is also a shape so and look i'm even getting the yellow underscore here and say that's not right visual studio code is helping me out there thank you so much visual studio code so that's the is keyword there is also the as keyword so i could say display t as shape and it gives me that it'll cast also into that type now if it can't cast into that type it'll return an error can't do that okay so um things to be aware of when you try and convert types so good questions there thank you so much um how is the linux support4.net core phenomenal it is phenomenal it's extremely fast runs great on docker as well um why did i use override asks are shia why did i use the override keywords good question i skipped right over that so let's talk about that here i defined the abstract methods for perimeter and area inside of a shape in order to provide the implementation right the specific implementation for those methods i need to redefine those methods in the child classes so here in triangle i've defined perimeter and in order for me to tell the c sharp compiler that hey this is the perimeter for the shape class i use this keyword here override okay so bit by by specifying override with the exact same signature the exact same return type and input parameters as the abstract definition here right see they've got the exact same signature except this one has the abstract keyword this one has the override keyword this is the this is kind of that pointer that says i'm implementing and defining this one for the triangle class that's why you use the override keyword and i'm realizing i'm seeing override in a very stadium announcer way um 3d polywrath says far is great but please don't do not overuse it um yeah where the used type where the result type is obvious is really where you should use it what if you cast t as dynamic sure let's do that real quick so dynamic is another key word that you can use inside of c sharp so if we say t as dynamic um it's actually going to give you the exact same thing right i still have the method calls there but it gives you the exact same thing if you cast something too dynamic um you're then doing reflection to go figure out all the methods and properties that you're interacting with makes it a little bit slower to interact with but it makes it a heck of a lot more flexible to call and interact with things but for that flexibility you're giving up some performance why do i want an abstract class if you're overwriting it anyway in other classes because if i need to have some sort of a some sort of an object that interacts with shapes i'm going to want to be able to call those methods regardless of how they're implemented and what the implementation underneath of them are i don't care about the implementation underneath i care that i'm calling a shape something else outside of my class is defining and directing and saying well you're you shape sorter class i'm going to give you a triangle to work with go ahead and do whatever you want to do with that but i'm giving you a triangle as the shape that you're working with i can show you a little bit about that with some of the repository code that i've written for clip talk in a little bit if we want to dig into that but i want to make sure we cover all of our content here because i've got about 45 minutes left to go here before uh before they kick me out um um all right let's uh move on here so you can declare variables or parameters in the base type and pass in or assign the derived type so this gives our classes more flexibility so if i define a method is quadrilateral right here exactly to those questions that we had right i want to be able to pass in a shape and do some sort of a test on it so not only do i have right i flip these back to just their simple implementations but if i want to be able to test inspect shapes and say well do something with this i could say well is this a quadrilateral and there's that is keyword return is a rectangle because a square is also a rectangle so i can test and say well is that shape a rectangle and there rectangle is quadrilateral true triangle is quadrilateral quadrilateral of course a four-sided shape triangle is not it's a three-sided shape so it's false so these are ways for us to define interactions that we do some sort of work with this generic object okay um clock is ticking yes what is the use of c sharp um i'll quickly answer that c sharp is used everywhere from iot devices mobile devices your favorite mobile phone tablet um desktop laptop workstation windows mac linux ios android works in all the clouds works on docker containers and kubernetes works all the places that you could write code you can build games with it and i'm sure there's a number of games out there that you've played that are all built with unity and c-sharp as the game framework uh that many folks use c-sharp is used everywhere and it's part of the fastest um web framework available check it out at tekkenpower.com for more information about that raspberry pi yeah we have our raspberry pi tower that i i have uh some c sharp running on here let me grab that real quick i don't have it plugged in right now but there you go there's my raspberry pi tower that all runs c-sharp i've got six pies in there so and it's waiting to be spun up with kubernetes to run all kinds of awesome.net code for me is it okay to use abstract inter abstract classes and interfaces covariance return types over generics uh roberto if generics have a very complex and nested dependency yeah you're gonna get into some confusion there i try to avoid covariance and contravariance concerns with my generics i really do you can do that i it's possible but it's easy to get confused so yeah we're going to get into virtual in a little bit here the thanos voice mod ah we'll get there how you doing broken sword uh you're very welcome i'm streaming today can we use static with abstract class and method yes you can a j yes you don't like languages with garbage collectors see plus plus as a garbage collector now get back to me um is unity full c sharp now or is it still mono uh unity is leveling up and getting more of um net core's feature yeah that's my tower of pies yep um the six pi zeroes equals zero nice i like what you did there chris um um reading the book broken sword is reading clean code mentioned a train keep trying catches near the surface layer yes we're gonna show that in just a little bit most languages do not have a gc thank you soviet doge for the fritz pogs let's talk about virtual methods here we're about halfway through so virtual methods are a way for you to define a default implementation of something so maybe we define a default implementation of area that takes length and width and multiplies them so i've got length width here so when i implement a triangle well triangle doesn't have length and width different so instead um i i have my abstract perimeter there it is perimeter and i have my area here well i'm going to redefine area and say this is how you should actually use area override and take this implementation because i have that default implementation up here i don't have to define it for rectangle and i don't have to define anything then for square because this already has length and width and when we execute creating a new triangle that works but what's weird about that is if i say display my triangle my triangle also has length and width that's weird so these are things that you can tinker with and decide if you provide a virtual definition for a method it can be overwritten but here's a default implementation okay we also have the ability to do sealed classes and a sealed class says no other class can inherit from this so i'm going to define shape with perimeter and area length and width and i'll have a rectangle that has a perimeter that has an area defined and i have a square while square instead of saying well just take the area that's there i'm just going to say well length times length but right um and if i execute that right if i say here's a new square with length and width of 4 i get 16. of course that makes sense but if i make my rectangle here if i mark this as sealed look at that i'm already getting a red line i'm already getting a red line right there look at that right square cannot derive from the sealed type rectangle and you might do this if you have some sort of a class that you don't want other classes to inherit from so i get all these errors look at this look at look at this mess because i'm not allowed to work with that it's sealed okay so get rid of the sealed keyword and it works all right so that's where i wanted to go with abstract classes virtual methods today for us to be able to interact with those um no that's not the uncle bob book clean code no clean code is written by somebody else i forget who wrote it um yeah my copies over there on that shelf um everybody should have a copy of clean code phenomenal book um the white font is so jarring what white font hey good morning jeff gamedev hello hello why am i screaming it why is the compiler screaming at us what about static abstract class shape does it work um no because if you put a static indicator up here on the class it's not a static it's right abstract types cannot be static um because you're telling it inherit from these things static classes contain methods contain properties constants innumerables that can be used enums that can be used in other things they're not intended to be re-implemented or created where an abstract class is something that you are defining should be created and be inherited from so um all right moving on uh oh you want to create uh okay yeah so i went through that and now that works so interfaces so we had some questions about this earlier about well interfaces are different so an interface is a contract it defines behaviors that a class or a struct must implement there is no right like the virtual well i don't want to there's a default implementation here an interface says you must implement these properties events methods indexers so we saw the innumerable interface used previously to define a an object that you could iterate over and you could take a look at each one of the entries in it so many c-sharp developers by a convention name their interfaces starting with a capital letter i so you might have an interface that defines how you log messages and by convention we'll call it i logger and i'll define one method in here log error it returns void and it has some error message that we're going to log and i'm going to also have a property here called error count now error count for those those objects that interact with an i logger can only get the error count you can't set the number of errors on the logger right we can just say well how many errors were there all right the management of the error count is going to take place inside of our logger so let me show you just a simple logger that i defined here for us to interact with and i'll come back to chat so i can define a display logger here that'll output things to the notebook display if if we tell it to log an error so i implement the interface and i specify that this class implements the interface using the same signature that we had earlier for implementing an abstract class right we have colon and the name of the interface i logger after the name of the class so in order for us to define and have those methods appropriately right so that it behaves as an i logger i have my log error method here and i have my error count defined as well now i put a static error count variable here right this field so that across all of the instances of display logger i'm counting here's the number of errors that i've interacted with and when you call log error i'm just going to say display this nice message and increment the error count i might have another type of logger that's a database logger and i might call it db logger so i still need to implement those those two uh that method and that property so log error and and we'll make some sort of connection now to the database and actually write that and i'm for the purposes of showing the difference here i just wrote display error log to database right the actual code to go right to the database is not part of what we're focusing on here it's the database logger and it's going to do something but i don't need to implement it to get my point across in this demo and i'll have an error count and and this might query the database to go get that count of errors and for right now i'm just going to return zero so i would then write some sort of a method display error count that receives the logger that we're currently working with and we're going to return look at that logger error count it doesn't care whether it's database logger or it's the display logger all that it cares about is that it's looking at an i logger and it's going to return that error account so maybe i create the database logger and i i say you know what let's log in error houston we have a problem go display that error count so if i run that it says error log to database and the error count is zero if i change this to the display logger right it also implements notice none of my other code changes okay i'm still calling logger log error because it's an i logger right i can even change this to i logger okay and i can pass that into that display error count and now i get the display logger saying houston we have a problem one there's one error run that again it still says one because i'm creating a new right i'm redefining and re-executing this each time okay so different ways for us to implement the same methods and we can put those interfaces we can put multiple interfaces on the same classes on one class so that it knows well you need to implement and be a logger and you also need to be able to do this you also need to be able to do that okay and that's how we manage and we interact with interfaces why two mice well this one controls this mouse that you see right here on this machine this mouse controls my um my prompter that i have that i'm that's my production machine so i can scroll through and read chat messages um thank you so much broken sword i'm i'm glad you're uh your team your group of streamers there enjoyed that have a have a great one more about the clean code book um look for it on amazon it is by uncle bob okay refactoring by martin fowler uh no no it might be i there might be another book that i'm thinking of then it's not martin fowler's book uh no i'm thinking of the gang of four book is what i'm thinking um how can we use static methods in a derived class so um let me go back up here to the shapes right so if we can create a static method in here um uh something like this yep so now down here right um i can also say display s dot i can't actually say s dot say hello right because that's on the shape object you can't call it on the instance you have to call it on the base type but it's there it's available for you right um right i couldn't call it from inside here if i'd like right i can do that and it's available to you because it is a static method that is available you can do that it right having that out there doesn't change too much of the behavior but you can't from a static method you can't interact with instance properties and methods it's in its own world off to the side okay um databases 101 that's a whole nother topic yes interface is like a contract that's right um let me see uh i see abstract class cannot create an instance correct so i think we don't need to use public modifier for properties and functions um you do because right if we don't provide that public modifier it defaults to private and you can see it's giving me a red underline here because they cannot be private you can mark them as protected that says that it must be implemented and the only classes that are allowed to execute that perimeter method are um down here the inheriting classes right rectangle can can execute that but you do need to specify public you can also specify internal that'll work also but uh public is probably where you need to be all right so the brief interface bits uh let me take a quick look here um does using interfaces only inside of a library make any sense are there any benefits no not too many benefits you probably want to publish your interface definition in one library in a series of implementations of that interface somewhere else so and to be clear introducing an interface and multiple implementations of a of that interface um does introduce a little bit more processing when you're on a smaller device like a tablet or a mobile phone or an i or an iot device that's an extra layer of processing and implementation to go through that you should probably avoid so is there a way that both twitch and youtube chats can be visible in chat yes um and i that's what i do over on the other scene but i this is what i want to show and i tried to do um something like uh feature chat to show a highlight here but i i can't get it to work um [Music] one real world example of an interface says surly dev is the power socket on the wall power companies all provided an agreed to interface it's delivering this much current over that that socket and there's an agreed standard as to the shape of that in your part of the world here in the states we have two prongs with a third circular pro we have two vertical rectangular prongs with the third circular prong on the bottom but in different parts of the world they're different shapes ours are typically square or square with a rounded bottom looks like a little bit like a v um of course right you've got all different shapes in different parts of the world and different current different current delivered across it as agreed to by the electric companies in in your country so yeah that's an interface and we've all got different outlets different plugs that we use to connect to those with our with whatever device it might be um all right um wouldn't uh jit usually remove the virtual method call overhead no it just in time compilation still has to fire you you'll actually do ahead of time compilation for an iot device or a um or a mobile device you're still going to cross a little bit of extra processing instruction that you don't need yeah every october i use ubuntu linux all month long so you can also provide explicit implementations of an interface this is something that is a little bit tricky i don't i try to avoid doing this because it makes it a little bit easier to see exactly what i'm working with but these are things that you can do so if i define an interface called i do a thing and it's got a method do the thing it's a public interface so do the thing is typically going to be a public method i can explicitly implement that interface here by by defining public void do the thing with some sort of thing now i can also hide it so it's not a publicly accessible method by explicitly implementing it so here's secret thing it's a secret and it it implements i do a thing but notice the signature here it's not a public void it's a private void i do the thing dot do the thing so it's saying this interface this method is available only when i'm behaving as i do a thing it's not available otherwise so if it's going to be behaving as an interface as the do the uh i do a thing interface then we can execute this so let's take a look here if i say new public thing i did the thing right that's the public void method there but if i change this to secret thing and implement secret thing doesn't contain that but if i specify that this is the i do a thing interface now it does know how to do that so the object kind of hides that it that it's implemented this interface and i know how to do these things but i'm not going to be loud about it and say hey look here's all the things that you can do it's hiding it and it's only available when we cast it into that interface type all right so that may be important for how you design your application it's an option you're allowed to do hey greg what are the implementations of i enumerable you can find it the i think the question is what are the properties and methods of it um you can you can find those implemented on lists and array and dictionary um i'm not going to go through that right now you can take a look at the documentation on docs microsoft.com and it'll show you all the things you need there um yes you can provide an abstract class with an interface yes so yeah all right now something that was added in a recent version of c-sharp i think it was one of the c-sharp sevens i forget if it was seven one or seven two but you can now provide a default implementation of an interface so this is a way for you to provide write some definition as to how something should behave now this is this is going to look a little weird i can provide an interface that implements another interface this is a way you might create a new version of that interface right so i've created i do a thing too and it implements all of the things from i do a thing so i'm going to add the method here do something else but it also i'm going to provide a default implementation of i do a thing here and it has a syntax that looks like that explicit implementation so write void i do a thing dot do the thing this is my default thing so now when i implement this interface i can say well here's a new thing and i don't have to implement do the thing i can say here's just do something else and it knows how to do that so now when i say all right let's try to do something else it does something else if i want to also tell it we'll go do that original thing doesn't know how to do that because it was explicitly implemented i need to just like we saw previously i need to turn this into if i turn this into i do a thing i do a thing too now it knows how to do it because i didn't provide an implementation inside of this class i was working with just the new thing object okay now that it knows that oh you're working with this interface i have that explicit implementation that i can then go work with okay so interesting way to version things provide that default implementation and i can of course right i can provide um do the thing display i did the thing okay uh now why doesn't it like this i've got an extra curly brace there there we go so now when i run it now i get i did the thing instead of this is my default so i provided a default implementation of it and i can override and still provide additional features and i'm doing this through interfaces so to the folks a little bit earlier who are asking about multiple inheritance this is a way to kind of get around that language limitation and provide some default implementations of things through interfaces which then can be in implemented multiple interfaces implemented on the same class um is linux a friendly environment to code and host c sharp yes um very much so it's one of the it's it's actually faster to run c-sharp uh applications web applications on linux than it is on windows um and the data is on the tech empower website when that third party reviewed and tested the.net.net core frameworks um [Music] you hate all of this what are the real world uses fraggle rock i'm sorry to hear that dependency injection is a big example of that we're you're going to see that in i think two weeks here on stream and and we'll show more of that i can actually show a little bit of clip talk source code how i do abstract classes and interfaces i'll show that in just a little bit here after i get through it now you know what i'll open it up in just a second um is the interface inheriting or implementing that other interface in this case implementing uh who who what um that's right you don't need to use all the features that's right quigley uh you are absolutely right um here i will let's open another visual studio code um let's do this just to kind of show you a little bit more real world example because i have it right here i'm going to open another visual studio code this is the website that i've been working on um on my other stream called clip talk and clip talk is a a pretty complex application here um that builds and serves information about video clips from the twitch platform so what i've done is i've defined things like where is it like a clip repository interface okay so i have this interface that says here's how we work with clips i know how to add or update clips i know how to add a bunch of clips a range of clips delete clips how to different ways to get clips i've even got way down here ways to update clips and you can see it over here i've got ways to search for clips fantastic it knows how to implement and work with these things and i don't pass implementations of the interface directly into my methods because i've also got things like a base repository class like this one for working with the mysql database and by default it has implementations here for there we go for get all go get all of this type from the database and it knows how to do that add or update this type a default definition of it you see i'm using the virtual keyword there to say this is the default way to add or update items in the database the default way right with the virtual keyword to add a collection of clips or whatever object is implementing to the database right get by row key get all you see these defined that are implemented by the interface but i add a couple more things on when i implement the full object like over here i have here's my twitch clip repository that knows how to interact with mysql and it implements my clip repository interface so when i ask for a clip repository it's going to get this implementation and it knows how to interact and use my sql because it is a my sql repository so it knows how to right i have an overwritten implementation of adder update here that i'm interacting with and i i reach in and call the rest of the base implementation the default implementation by calling base adder update and it calls into that class i inherited from and says execute that adder update method and i've got other methods here that are implemented get clips for this channel and it knows how to interact with the mysql database and then do that execution so right here's a little bit more real world example of how i'm interacting with this consequently in my azure functions right so i'm up here in my azure functions and if i go to here get clips i like right i have there i'm referencing my clip repository and i'm saying get clips liked by my twitch id and format them appropriately with a little bit of link statement here so this if i mouse over a clip repository you can see is an i clip repository it doesn't know that it's a mysql repository it doesn't know that it's a different type of database repository it just knows that that class knows how to get clips liked by this person fantastic i don't have to think about it i've hidden i've encapsulated all that logic in another class where it can be managed appropriately all right let me head back over here and let's get into error handling all right we can talk more about about clip talk tomorrow over on my stream um twitch tv slash c-sharp fritz um unix is faster to upload data in my sequel over windows by a sizable amount i can't i i can't rule one later comment one way or the other on that john on youtube um yes interface is just signatures i do stream i am streaming currently on twitch as well shophem we i actually streamed to three locations for this stream twitch tv slash visual studio youtube.com.net and we're also on learn tv aka dot ms slash learn tv you can find me over there as well um did i use the serverless layer yes i did for clip talk um am i doing this method default hiding stuff anywhere no i am not how relevant is a factory pattern in today's world of.net six i'm not using it anywhere but there are folks who who use the abstract battery factory pattern sure how relevant is it hard to say restful methodology for this api over in clip talk yes entirely um did i use serverless layer there is no controller it's it's a class that has those objects injected into it what shade are my glasses these are gunner lightning 360s that i'm wearing where's mixer mixer was discontinued more than a year ago by the xbox team okay error handling and exceptions there were folks asking about error handling and how do we do with the deal with this when you do something that you shouldn't right so i have a divide method here if i try and divide by zero you get an exception and that's ugly nobody likes that what do we do with that we can handle those exceptions we can do something with that by wrapping our statements in a try catch block many other languages have try catch blocks and there were folks who were commenting earlier and they're right you want to wrap as little code as possible in a try catch block you don't have to wrap everything in a try catch block you should be writing defensive enough code that you don't need to write a try catch block typically you're going to put a try catch block under something that's working with external input right something that a user submitted something that was fetched from an external api or database or something and you want to check that because right code that exists and interoperates inside of your application you should be already defensively coding against doing appropriate checks to make sure that you don't do anything out of bounds but when it comes to working with things that are trusting external input we should wrap that code in a try catch and when you start wrapping things in try catch you're adding on to the stack of processes that net is managing in memory and if you have more and more of those it becomes a real a real burden on the processor to unwind and interact with those as errors happen and it figures out what to do with things so you want to keep this as shallow as possible only right try catches around things where something could really happen otherwise practice some defensive coding write an if statement around something or a switch case so that you properly test before you start pro processing and working with you um with input okay so in this case try catch we're gonna instead of just trusting that we can divide those we're going to instead say we'll try doing the delete and we're gonna handle a divide by zero exception and if there was something else if something else happened we can catch and say well this is the this is the catch-all that we're going to go and display some other message so we're going to display that if we try and divide by 0 and return 0. so if we just say divide it works if we try to divide by zero it errors out if we try to divide by null you broke something else so because you can't divide by null it doesn't know what to do with that so that catch all this catch statement without an explicitly defined exception type catches that and we can interact with it now by defining the exception type here in these parentheses i can actually inspect that exception and do something else with it if i'd like all right and that's up to you what you want to do with it but what's important also is that you you clean up now i hope in this very contrived example you see here that instead of instead of catching when it when it divides by zero or when a null is passed in you could put a an if statement in there to behave appropriately and raise an error or we call throw an exception that says hey you did something you're not supposed to do and that will actually perform better than adding more try catch blocks here so if i want to clean up any extra things that are going on i can use a finally claws and this finally block here will clean up and take care of anything that you might also want to uh ensure happens before the exception is passed on or something else occurs finally always executes always always executes so if i say divide four by two i still get it saying cleaning up code even though it didn't catch this error that finally executed and if i change that to a zero right you broke something else cleaning up code here and you see how it says throw it's taking the exception that was caught in this block and saying okay throw it out somebody else above me somebody who's calling this method is gonna handle that exception and do something with it so we throw it okay so and i'll change that to and if i put null in there right i'll get the same thing all right so there we go and you can create your own exceptions if you want to be more verbose in how we interact with things i've got three minutes left chat so i'm going to run through this real quick if you wanted to find some other exceptions that have better information or a little bit cleaner defining what went wrong i can do something like create a better division exception and we saw about base classes well there's a base class for an exception so i can provide better division exception and it's got different constructors here that i can work with so i'm going to provide a message here unable to divide by zero if we get the divide by zero error and we can also provide a different message and an inner exception if something else happened further down the stack and we have some default information that we're returning but i'm also going to provide the dividend and divisor as properties on this exception so that i can inspect it later if i'd like so now right i can put that if statement in here like i was talking about so if the second argument is null let's throw a better division exception and say cannot divide by null and check this out i'm going to um wander over here i'm also going to include the dividend and the divisor this way right when i go and interact with this i can actually get additional information so now it tells me when i execute and i catch a better division exception it says cannot divide by zero and reports back information right this might be more helpful for you to include in your logger so you can inspect and get additional information now i'm once again contrived example but when you're working with things coming back from a database and interacting with it this could be very important let me head back over to the desk we're going to say goodbye to our friends that are watching over there on learn tv thank you so much for tuning in really appreciate you joining us it's been a lot we went through a lot here today and that was we had a lot of really good questions i encourage you to check out the um the github repository because it has the information it has the notebook there for you i'm even going to link directly into the notebook there it is there's links if you're watching the youtube recording just below for you to grab that notebook take a walk through it you can run it on visual studio code on windows mac or linux and learn more try it out try out some of these interactions and and learn more about how this works i really appreciate you joining me um on this fine monday and uh i'm going to take a quick look here at chat um let me see cool all right let's call it a day thank you so much to all of you out there watching on youtube i appreciate you joining and appreciate you tuning in check out all the great videos on youtube.com slash.net i've got a series there about blazer this video is in the c-sharp with c-sharp fritz series make sure you check out some of our other shows like on.net in our community stand-ups those of you that are over there on twitch let's set up for a raid we're going to take you from our visual studio channel i'm going to magically whisk you to another channel on the big twitch.tv network get you connected with somebody else who's broadcasting and having a good time writing some code and i'm going to take a quick peek over here hey that's me you know what um somebody's asking about [Music] uh no let's check out who's in software and game development that we can read that's not bad [Music] taking a quick look to see if there's anybody here that we want to support who's doing some cool.net stuff now you know what i think i'm gonna take you over to our friend tim who's an independent developer writing uh he's building a c plus game engine and he's porting it to web assembly so let me get you connected with tim beaudet he's a live coder a member of the stream team that i manage here on twitch thank you so much for watching i really appreciate you joining in i hope the rest of you tune in sometime later today the.net docs team i'm sorry the microsoft docs team is going to be hosting their show talking about cool things happening in documentation samples um and a little bit later today our friend um matt's christiansen is going to be hosting visual studio office hours i hope you tune in for that thank you so much friends i will see you next time get ready to say hi to our friend tim over there on his channel working with c plus and webassembly take care [Music]
Info
Channel: dotNET
Views: 6,243
Rating: undefined out of 5
Keywords:
Id: 3aj5Wz1f9xY
Channel Id: undefined
Length: 121min 49sec (7309 seconds)
Published: Mon Oct 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.