Intro To Class Libraries in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the class library project type in c-sharp is probably the most important project type in c-sharp every other project relies on a series of class libraries but with it being so foundational it's often overlooked in this video we're going to look at what the class library is how to use it in various projects and how to get the most benefit out of it now if you don't know me my name is tim corey it's my goal to make learning c-sharp easier i do that by providing videos here on youtube multiple times per week plus i have a weekly podcast i also provide courses on c-sharp web development and much more at iamtimcorey.com the profits from those sales are what pays the free content here on youtube so that everyone can have a great education in c sharp not just those who can afford it now in this video as with most of my videos i create some source code if you want to copy that source code use the link in the description all right let's jump over to visual studio and let's get started with the class library project so we'll say new project and i could just jump in use class library notices at the top of my list because i use it quite frequently but let's do this instead let's search notice my my settings i have c sharp as my language but everything else is is standard let's search for class library and a number of different options come up so we have just class library and that is the one you'll use most often this is going to be the the.net 6 or whatever version of net you have library that's the standard one you'll use for all new projects so it's kind of like the the generic one the one that is most useful but there are other specialized libraries available for example the razer class library is for just the the blazer type stuff and then i'm sorry not blazer it's for razer pages stuff which is asp.net core so you can use it for that and that's going to be ui specific class library so it'll be for user interfaces not for just uh just code it'll be ui specific code so we'll talk more about that a little bit and on the theme of ui specific we have a wpf class library so again this is more targeted towards a user interface library and a class library for universal windows or uwp again for that ui library and then we have class library for net framework and this has been a confusion point that's gotten better over time since we have gotten further and further away from net framework but there was a time when the class library project was just called class library and it was net framework and then we had a time where we had the class library project that was net core that said net core we had one that said net framework we had one that's a net standard and we had all these different types and now we have again the generic one that comes up is the dotnet core the dot the dot net which is still.net core but we just dropped the core off the name so now it's dot-net six or five or net seven whatever version you want but we still have the.net framework version so if you want to work with a net framework user interface so let's just say you have a net framework 4.7.2 application well you know it's a winform application you've got this user interface and you decide after watching this video that you want to create class libraries that support it well since you have your user interface in the dot net framework you should probably create your class library in the dot net framework as well because the two have to match so if you're using a.net core project then you'd use the original class library which is a.net core project type so we want to match this off one to one there is an exception for that and that's a net standard which we'll see in a little bit i'll talk more about that in a minute we have the android ios android these are again user interface specific and win forms or windows forms android binding ios binding again user interface specific and then more user interface specific but there is a um net standard class library ah nope so they change it again it used to be that when you said class library like i said it would be in parentheses you'd say dotnetframework.net core or net standard now when you select this if we hit next here well we'll do a minute but when you hit next you can do a net or net standard so you have the option of either so remember i said that if you've got net framework user interface and you want to use a.net framework class library the exception to that rule would be if you're looking to upgrade your user interface over to dot net so if you want to upgrade it into the modern version of net well then you would probably create your class library as a net standard 2.0 class library that way you could still use it with the.net framework but then you can also use it with a net core project it's kind of a bridge project i do have a video covering.net standard on this channel you can go in and look more about that but there's a lot of different options here we're going to focus today primarily on the class library so a.netstandardor.net uh version of the class library the generic one the user interface specific ones are just mainly there to add additional things to make them more useful to the user interfaces but you could use just a generic library too so we're going to start with the class library the one that you will use almost exclusively we hit next and let's give us a name so we'll say demo library i like to name my class libraries with the name library at the end and the reason why is because when you add a reference in your user interface to your class library you don't want to add a reference to another user interface you want to make sure you get the right library to add a reference to it we'll see that so by saying library at the end you know that's the thing you want a reference because that's a class library so that's my personal opinion you do not have to follow that it's not a industry standard thing that's just a thing that i do okay so let's put this in my temp folder and we'll call this demo app we hit next and here we get to choose and notice we have net standard 2.0 and 2.1 and again if you're working with net framework and wanting to upgrade to.net core you'd want to choose.net standard 2.0 not 2.1 2.1 does not support net framework so again i have a whole video on.net standard we're not going to cover that in depth here i do want to point that out the higher number is not mean better so i have the option of net core 3.1 which is long term support or lts dot net 5 which is out of support and net 6 which is the new long term support so net core 3.1 i believe that goes out of support in december of 2022. so there's a uh just over three year cycle for the lts or long-term support versions and in a month support window for the um i've heard they call it the the other versions the versions that are um just they're for the people who want to upgrade all the time you get the latest features all the time and not for businesses who want that long-term support and that longer period of time to support a version so um definitely financially use this but i would upgrade.net six if we can but we're gonna start in.net six that's the latest version the one we're gonna use we'll hit create and notice it does is supported by android linux uh mac it's it's a cross-platform project type so you can use this class library on pretty much any platform because it is net version 6. okay so right out of the gate this is the class library we have our name space we have our class one as our default the reason why we have our our class one here is because when you were to add this as a reference somewhere else it needs to have some type of class already in there otherwise the name space does not exist so that's why we have class one i hate class one i wish it wasn't here i wish they would just start with nothing but i do realize the reason why they did it this way is because if you don't have a class in the namespace then the namespace does not exist so that's why i did it that way now just so we were clear here a class library project on its own does not run you cannot just hit the play button in fact if we do that we're gonna get this message pay attention to this message don't just skip over it i want you to actually look at what it says it says a project with an output type of class library cannot be started directly okay so when you see this message that means it's you've got the wrong project starting if you're trying out run a class library then you make a mistake that's not what you do with a class library you do not execute it directly so if you see us this error message that means you need to change which project is the startup project you want to have a user interface project of some kind so that's a console app a winform app wpf asp.net core mvc razer pages blazer server blazer webassembly whatever the the user interface is you choose you want to have that as a startup project not your class library so let's talk about what is a class library why do we have class libraries well class libraries have a number of different purposes i'm going to share those purposes up front we'll see some of them in action some of them i'm not going to demo but i'd love to know if you want to see demos of some of these other things so one of the big things that class libraries allow us to do is reuse code so if i create code in here and we're going to do this we're going to create a little bit of code in here if i create code in here that is some type of value add for my application if i put in here in a class library i can have multiple user interfaces call this library and use the same exact code now what benefit does that have well by doing so i am conforming to dry don't repeat yourself now there's a limit there you be very careful to walk a line dry is not a um dry should not hurt you it should only help you so it's starting to hurt you then stop doing it repeat yourself but if there is a a case where maybe you have some logic for example for me a big repetition is data access logic if i'm accessing a sql database well that code is pretty much the same for every one of my projects so i put in a class library and just reference that library so what is a library it really is what it sounds like it is a library it's a repository where we can ask for uh ask for code ask for methods and say hey give me this method we use this all the time in.net so if i have a actually put in class inside class one for now we're going to delete class one a minute but public void run me and i were say console.writeline now we're not in a console application this is a class library but let's just say that the caller of this was a class library application well then this right here console writeline this is a method this is a class where did it come from it came from a library microsoft has created a bunch of libraries that we can use in fact we can add more if i were to right click on dependencies and say manage new get packages and go to browse well there's a lot of new packages that will show up in this list so entity framework for sql i've got newtonsoft.json i've got system.txt.json mock nollies are from microsoft but these are all libraries these help me do different things so by adding one of these things ceralog is a great one that's a logging tool well it's a library and they have written a bunch of code in a class library and then given it to us to use and i can just say install this and then execute or call those library methods whenever i want well that's code reuse that is not repeating ourselves we create the library once or someone does and then we reference that library over and over and over again microsoft has broken out net core into a bunch of smaller libraries so we don't have to have every library built into net core in net framework we had a massive framework because everything was built in or a lot of it was with net core microsoft said hey we're going to break that apart into smaller libraries and then we're not going to include everything in net core we'll have nuget packages and then if you want for example entity framework core well then you can install that nuget package and then if you want record sql with nt framework core you'll install that nuget package which is a nuget package is just a wrapper around a class library so we use libraries for every single application we built let's see how we can build our own application in just a minute but that's one reason we use class libraries is for that code reuse but there is another thing that is really beneficial about the class library is it allows us to disconnect our business logic our data access from our user interface code now again you have to make sure that this is not a hard and fast rule but more of a best practice or guidance and that is you can separate out your user interface from your business logic and data access and other types of code like that the benefit there is that we can upgrade our class library separate from our user interface we can also create a different user interface and call that same class library that's the code reuse part but we've got code reuse it's a big thing with class libraries be able to be disconnected from a user interface and upgrade one but not the other and then there's the idea of of organization we can put our similar code in a similar library we can have multiple libraries this allows us to organize our code in a way that's logical with small applications we should probably only have one library but with larger business applications we may have 4 5 6 10 15 libraries depending on how large the application gets this allows us to independently work on these class libraries to have them grouped by logical thing maybe one is for sql data access maybe one's for mongodb data access maybe one's for certain business logic whatever the case may be so that's another use for class libraries and another one that is used occasionally it's not as big of a use but it is something that's really fun to do and really nice to do in certain circumstances and that is it allows us to create plugins for our application we can create an application a user interface that has certain hooks in it and it can load dlls which are class libraries compiled they can load them and add that code to the user interface dynamically using what's called a plugin system we do this all the time in visual studio notice extensions up here what do you think that is that's libraries that we add new code to visual studio so if we come here to manage extensions there is a bunch of extensions that are installed github copilot is currently installed to have it turned off but for this demo at least i'll turn it on a little bit but we have live share we have all these other extensions for our visual studio these extend or add value and functionality to our user interface well that's done through class libraries so the class library is useful for a lot of different scenarios so let's talk about how do you build a class library what is a class library and so on so we've already seen we can't run this directly but let's open up let's go to open folder and file explorer and we'll go to the bin debug.net 6 and in here we'll see that we have the demo library.dll this is the class library this is what it's compiled to normally when you compile a user interface that's a let's say wpf project it would be an exe project if you're on windows which wpf has to be so that's a executable file that can be run directly a dll is a dynamic link library if you look on windows you'll find that windows has a lot of dlls that it uses for various purposes well the dll for our library here is is the file that gets created now pdb and the json file those are separate and they're different they're support files but the dll file is our code it's our library but it's not useful in that state we can't execute this library so what do we do the library well we create we add it to a user interface so let's first create a class let's delete this one let's create a new one and we're going to create a class it's getting simple stuff we're not going to do you know real code and data access to all the rest we may do that in a future video but for this we're just going to do simple uh simple code so we're going to say let's do a person processor and let me get rid of all my using statements i'm going to change to file scope namespaces so i put my semicolon here and that what that does it saves me four spaces if you undo this notice how everything's indented four spaces or whatever the tab uh stop is here for you but it's all indented by the inside of these curly braces so with that that doesn't really add a whole lot of value if you don't have more than one namespace then why are we indenting everything inside that namespace so if we use the file scoped namespace if i put a semicolon here it takes those credit braces away everything's over on the left-hand side so now we've saved four spaces for every single line in the entire file maybe just a little bit of horizontal space reclamation i personally prefer it let me just public and we'll create a public let's do a public string and we're going to say um join name string first name string last name okay and this method really simple stuff we're going to do here it's going to return uh first name plus space plus last name okay so that's just going to join those two together and create a full name or something like that so again this is not rocket science this is not something you would need to write down make sure you take notes on because this is just a demo to to show off we can do the class library so i have my code here and this is something we do let's just pretend this is something we do in a lot of our different applications we we always have a first name and last name but we need some way of joining them now obviously again this is a demo but let's just pretend it's some kind of logic that we do in a lot of our different applications well we can put in our class library now let's right click on our solution and say add and let's create a new oops right click add new project there we go and we're going to say a console app that's a user interface so console ui like so and again.netstates which matches our class library and we can get rid of this actually let's get rid of all of it there we go and now what we're going to do notice we run this it doesn't run because we're still setting demo library as a startup project the first project always is the startup we can change that by this little drop down here oops not that one yes this one nope okay sometimes i'm not sure why it's not showing up sometimes it shows the other project type that can select it but you can right click on console ui and set a startup project there we go so now we have that we can run this which is not going to do much uh there's build errors program does not contain a static meth main method um there's no code in here basically console.writeline hello world so you run this now it will run it's gonna give us this hello world and then exit the application not exactly a um amazing application but let's just say we had um well let's do this console.write what is your first name colon space and we're going to say string first name equals console.readline and we'll do the same thing with last name and it's at it's yelling us because this is a nullable string so let's make it market nullable and what is your last name and then we're going to do that join and then say your full name is but we could put the code we have you know this complicated logic we could put a complicated logic in every user interface but maybe we use it over and over again so we're going to put in this class library but how do you reference it over here well we right click on dependencies of the console user interface and we say we want to add a project reference and select demo library this is where that naming convention comes in remember i said that i like to name my class libraries with a library at the end that way we know which project to select now there's only one right now because i'm in one referencing another one therefore there's only one in the list i can reference but we're going to see in coming up soon they will have multiple options you do not want to reference the other direction so your class library should not know about your user interface your user interface should know about your class library that's the direction it goes which means the code in here should not talk to the user interface directly because then it knows about your user interface now there are some um user interface specific libraries we saw those like the wpf library and the razer pages library and so on where you can you do user interface stuff or user interface code in there because it's a user interface library it's a little different that's specific to a type of user interface but in this case the generic library you don't want to know about anything to do the user interface so i'm not going to do a console writeline here because the fact that i might not have a console in every user interface instead i return that name and then allow the console to write a lot right still one so in program.cs i need a using that's demo library that will allow me to say string full name equals and it's a person processor oh i need to make market static um let's not worry about instantiation person processor dot join name and first and last name so go and join those two names and um yes because these are potentially nullable so let's mark these as nullable and like so so so now we have a full name and i can say console write line string interpolation your full name is full name like so again not exactly a rocket science type application but this should give us a working application what's your first name tim last name is corey your full name is tim corey cool that works okay but what value to the class library at now let's pause right here let's not think about other user interfaces yet let's talk about what value the class library has added so far well the class library is doing non-user interface specific code personally and this is a personal preference but i'll talk about why it's i think of value personally i like to move every bit of code possible out of the user interface and into a class library now in some cases that's just moving it because it it allows me to kind of just move things out and keep this user interface as small as possible so this is user interface specific code asking for their name capturing that name asking for last name caption that name and then the code that is not user interface specific we call out and give the information needs and say hey do that work and give me the results and then we again do user interface specific stuff so the user interface specific stuff is done in the user interface but everything else has done the class library if you did not have any other plans to reuse the library i think this is still valuable because let's say i want to upgrade the the console user interface maybe i want to change how it works well the console code is simpler it's just the console code so let's say a microsoft comes up with a new version of console or the new way of doing console write lines well we can just change the user interface specific stuff in the console and not worry about all the code we don't have a thousand two thousand lines of code in here that some of its business logic service did access some of its user interface it's not all mixed together in one big ball of spaghetti instead we have our separation here that allows me to do work on just the user interface specific stuff let's just say that down the road my boss comes to me and says you know that console it's not real pretty we want to upgrade to a different user interface well if i have separated out my console code from my non-console or non-user interface code then i just have to change the console project i don't have to change the project and the business logic and the data access if you have it all mixed together like one big bowl of spaghetti well then it makes it much more difficult to upgrade that user interface or change that user interface i know of a lot of situations i have been through so many where people said you know what it's just easiest to put it all in the user interface and then down the road they said we'd love to go to the web we've got this desktop application we'd love to be on the web but we just can't it's just too complicated to extract out and replace just the user interface specific code because it's essentially rewriting your entire application but when you have moved most of it to a class library well that library can work with a new user interface you don't have to redo all your user interface code and all your business logic and all your data access and all the rest of code you have you just have to replace your user interface code so that makes for a much easier transition so even if you only have one user interface i highly recommend you use a class library for everything that's not user interface specific now are you gaining any other benefits by doing that probably not that's probably the only benefit you get is that separation where you can upgrade one and and still have the other one support that new version so you can replace console ui with winform ui or wpf ui and it makes for an easier upgrade process but you also have some organizational benefits so you can kind of organize things a little better but besides that that's maybe it maybe you don't have any other code reuse maybe you're not calling this method more than from just your one user interface and that's okay you're still getting benefit out of it but let's just say that you do decide we need a new user interface so you recline the solution and you say add and we're going to say just for the sake of this demo to make it faster we're going to say winformapp and we're going to say winform ui and net6 and now i have this new fancy user interface that allows for drag and drop building whenever whenever it loads so this this application let's make a couple of tweaks here right away um let's change immediately the font size has to change nine point is ridiculous let's change this for dental purposes to 22. and now you do that and make the form extra big shrink it back down okay i'm not gonna make a pretty pretty beautiful form um i'm gonna make something that that works okay so let's create um let's get our toolbox here wait for it to load it's been a while since i used window forms so it's probably buried in the back of my hard drive but we're going to do is we're going to use this winform ui to replace our console user interface so instead of having the um the console ask us what's your first and what's your last name we're going to use uh just fields so we're going to grab a label and let's pin the properties here and we're gonna again do this really quick so first name i only name this really first name and let's grab a text box and we'll align that up with the text and make it a little bigger and we'll call this text box first name text and we'll copy these things and paste them wait for it to load the new winform designer is a little slow i have noticed that so let's call this last name i can get a little bit i really hate doing things this quick on demo for example i'm not naming these so this is label two this is label one i don't like doing that but um for demo purposes it is what it is um it's just the fastest way i can do things whoops last name text okay and then we'll have a button sounds good and we'll call this the um combine name and oops under here text where i say combine name double click it and in here let's make our file scope namespace in here we can in its method we can call our same method from our class library but again we have to right click on dependencies add project reference and here's where that that library naming convention comes into play because i could reference my console ui that's not right i want to reference my demo library so i click ok and now i can add my using up here using uh demo library and i can say in here um i think it's message box it's been a while dot show and the message is going to be i'll put well i'll create a variable for it string message equals or actually full name and i'll say person processor dot join name and we're going to say first name text dot text that's the actual this is the text box this is the value of it and then last name text dot text and then we'll say string let's get this almost on the screen here um string we're gonna say message equals string interpolation and let's grab that same um let's get something similar this well let's use the same thing be lazy there you go same kind of message and we'll just show that so when you click the button it's going to pop up a message box that says here's your full name now i'm still allowing the class library to do the work i'm just doing user interface code here and this just makes my user interface code a little cleaner instead of i could put this right in here that worked too so in fact you know what let's do that because it's not that much different we don't need to allocate on a string so with that we now have our user interface code in here but we're still reusing now that class library so if we run this we have to go back over here to run it we have to right click on winform ui and say startup project and let's run this and we get a windows form we can say tim corey combined name that says your full name is tim corey so it's the same code we have reused all that complicated logic we have in the join name method but we reuse all that logic in a new user interface very very quickly and easily so this allows us to switch between user interfaces the really cool thing is that it doesn't have to be the same style of user interface so we've got two different desktop apps well i could add a blazer server project and reference the same demo library and call the same code this is something that really gets people confused sometimes when they watch me create demos for example with data access where i'll create a little class library that has sql data access and it it talks the sql database gets you know data in data out all the rest and i call it from a user interface let's say blazer server people ask me well how would you do that same user interface or that same data access with windows forms well guess what it's the same class library the same way so we whatever code you can put in a net six class library you can reference from just about any user interface and i say just about because there are some limitations or exceptions the biggest one is going to be blazer web assembly because it fully lives in the client side and so your class library has to be fully client-side ready code so usually data access can't be done from client side therefore you couldn't call that class library but anything server side anything desktop console those kind of things services they can all use the class library directly the other limitation is blazer maui or blazer hybrid or dot net maui because both of those are they are desktop applications and they can use some desktop dlls but the the mobile versions can't so you couldn't use it for the entire application because mobile can't have direct data access either but it can access other class library stuff so if you had class libraries like this where you're doing string manipulation yeah you could reference that from a mavi project not a problem so it's just it depends on the type of code in the class library for certain project types and i call those out when i work through those project types but for most project types a class library can be used across them so a data a sql data access class library can be called by blazer server by asp.net core nbc asp.net core razer pages asp.cor api jpf win forms uwp win ui 3 console worker service the list goes on all the different places they can call and use that data access library so even the more restricted types most of them can use it so grpc can also use it so that class library is super helpful notice that i haven't redone this code code is still in one place i've been able to call this class library and build a new user interface that supports the same class library same business logic same data access code same whatever and i just built the new user interface when i'm ready i can replace the console ui with the winform ui that is just replacing the user interface not the entire application so yeah no the win the user interface is not the entire application or it shouldn't be you should have the ability to separate out your business logic data access and so on and just have ui code so that's a really useful tool is if you put your code into a class library then you can make your upgrade process for your user interface or the change process much easier let's just say that you want to have both user interfaces no problem you can still do that and you can still build out this class library with more and more things to support both user interfaces i have found this very helpful when doing things like maybe a desktop application or something similar but i also want to have a an api maybe maybe i have a blazer server application so i have blazer server cool but what if i wanted to support a mobile application well i'd want an api i want that api to have access to pretty much the same stuff that the blazer server user interface does well if i put everything in a class library the api can call it and the blazer server can call it that way i can have two different project types and then support even more through my api but they can both use that class library i have centralized most of my code as just the user interface that's different and the user interface really is a small part of your overall application yes i know a lot of work can go into your user interface a lot of uh time and effort goes into making it look good and adding the right controls in the right spot but it's really besides making it look good it's the data access and the business logic and all that code should be in class library not in the ui so that's the benefit of a class library we haven't even touched on the fact that we can use a cloud and nuget package and i've talked about it but how'd that actually work well right now both of these projects reference the demo library directly but what if i want to create a new user interface project in different part of my my system is not in the same solution how i reference the same demo library well we could create a reference to this demo library and have it just be on a different spot in the disk and that could work but it gets a little messy or i could just so you know i could take the dll remember we uh whoops not terminal if we look at the the bin debug dot f6 we have that demo library.dll i could take this and just copy just the dll and put it in a different folder and reference that dll so that's possible too that's not ideal but it's possible so i could copy this and put it into the console ui and and say in the bin directory debug i just paste in demo library which notice it's already there because the reference actually makes it copy the dll over so i could manually do that i could copy it other places but then what happens when you have different versions of your dll where maybe you copy the latest version your dll over to a project get started you build project up in the meantime demo library has changed how you get the new version we copy the output dll over to the other project update the reference well what if you haven't done that in a while it's kind of scary right because what if things have changed what if we change the logic of joint name it might break my new application so maybe i don't want to use that version well now i've got to figure out which version of that that dll is it because it's the same name so you have to figure out is this version 1 or version 2 or version 2.5 what version is this and that gets kind of messy and that's where nuget packages come in because you've if you've done c-sharp for a while you've probably used nuget packages nuget packages are the way to get these libraries and their way to track the version you're using and even use a specific version you don't have to use the latest version of a nuget package let's right click on dependencies in our demo library go to manage nuget packages and let's just say we're gonna this is gonna be our data access so i'll select dapper well notice the latest stable version is 2.0.123 but i could roll back and say i actually want 1.60.6 i can select that version and install it so whatever version i wanted that's still available i can select so i can even go back to the version 1.0 of dapper now is that the right call probably not but when i'm on an older project it may have used 1.6 and there was some breaking changes when he went to 2.0 so maybe i'd stick to 1.60.6 for a while and use that version and that's okay but that allows me to choose which version of the dll to to use and instead of manually copying on dll we use nuget to download it for us and yes this downloads dlls and puts it into our project so that's how we deal with sharing libraries in a way that is easy to use even internally so you have your own internal nuget feed notice this package source up here right now this is the publicnewget.org but you could add to your feed you could come in here and say i want a new feed and you could put in the url or file location for your packages so if you have a private package feed you could put that here and search that one as well so that's how we share libraries between projects if it's just if you're just building it for one project you just right click and say you know reference this which we've done here so we said add project reference we've chosen the demo library that's great but if you're gonna use it quite a few times you're gonna use it inside of an organization you probably want to wrap it up in nougat package and that way you can version it you can have different versions for different projects and you can make sure that you have it in one spot with all your logic have that dry don't repeat yourself build the logic once and then reuse it as many times as you want throughout your organization we've talked about dependencies but i do want to point out how this works so when you build just a demo library it creates that dll when you build just a console ui it didn't have any references it builds just an executable file but what if you have a console ui that has a dependency on demo library well what happens is it first goes out and says what are my dependencies and says oh i depend on demo library.csproj okay that's a dependency that's this reference so it says okay before i can build out my executable file for the console what i'm going to do is build out the demo library so it builds a demo library gets the dll from the bin directory it copies it over to the bin directory for the console project and says okay i have the library now i can build the console project which depends on that library and that builds the console project why is this important to know well what if i went to my demo library went to references add project reference and selected console ui maybe i say you know what i think my class library needs to know about the console and i select this and i hit ok and it says a reference to console ui could not be added adding this project as a reference would cause a circular dependency why is that a problem well because remember how the build process works console ui says i depend on demo library so build demo library first and then give me the dll then i can build but what happens if demo library says i depend on console ui so build that first and give me the exe well if i come back up here to console ui it says no no i depend demo library and demo library says no no i depend on console ui neither one can get built because they both depend on the other one first so that's why we can't have a two-way dependency we wouldn't want that anyway and here's why if you decide that your code must know about the user interface project in this case console ui if it must know and have a reference to console ui guess what you have console ui code in the wrong spot that's console ui code and it should be in the console ui project if it has to access the console ui project it needs to be in the console ui project so if your library code needs to have access to the console first of all ask yourself does it really but it really does then you need to put it in the console if you've got it in the wrong spot it shouldn't be in the library it's not adding value in the library and it's just put in the wrong spot so your class library should not know about the user interface and the reason why is because the fact that right now we start off with console ui calling this method but what if this method instead of returning a first name what if it did console.writeline firstnamespace lastname well then we create the winform user interface and we come here to the to the code and we say i want to put that in the message box and it goes no no i'm going to do a console.writeline well what does the console.writeline do for form one nothing in fact let's um let's comment this out and let's take the same code and do a console.writeline here instead imagine this is what's being called in our class library well if i run this and i say tim corey combined name cool it's done but um nothing happened seemingly it did if you're actually running visual studio but you can go to the uh what is the output window and i believe it's in here um somewhere buried but um it's the only windows but you shouldn't be doing that you shouldn't be using your um your console for a winform project because the winform project is supposed to show stuff to the user and there is no console that's being shown at the user so this this demo library shouldn't be talking to console shouldn't know the console because it's not always going to talk to the console instead sometimes i'll talk to a console like in console ui but sometimes it'll be talking to the message box in the winform project or maybe it'll be talking to the web page in mvc or in razer pages so your user interface i'm sorry your class library unless it's a user interface specific class library should not know about the user interface at all and even if it's a user interface specific class library it shouldn't know with a specific user interface so even if i had a winform specific user interface library so it can have winform code in it it should not know about winform ui it should just know that it can talk to windows forms and have windows forms uh code in it that's all should know about because the whole point of class library is to be able to call it from a different project so if it is tied directly to winform ui only well then it should be in winform ui that's where it should be so that's how to use a class library that's how to use or create the dll you can use a dll just directly by using a project reference like you've seen here you can also copy the dll if you wanted to you can then right click on dependencies and say it's under i haven't used a long time i think it's shared project uh oh browse right here so you can browse and say browse and then look for a dll which of course these are like blend dlls but you look for your dll and reference it that way directly don't do that because that just creates a mess if you decide you want to have that dll go around at different projects and not be in the same solution well then you want to create a nuget package and have a new package feed for yourself then you can reference it from a bunch of different projects by saying manage new get packages that way it's very very easy to reference very easy of version very easy to update those versions it just saves you a whole lot of headaches to do it that way and then finally dlls can be or class libraries can be used for things like plugins for other applications so really useful stuff there as well i won't get into head specifically if you want to see a plug-in video go to suggestion page the suggestions.i am timcory.com i believe is already a plug-in suggestion there go out vote it um let me know if you want to see that but this is the class library project type and again this is so useful for so many different projects i highly recommend that once you've learned the basics of c sharp then i would get i recommend you get into learning about the console i'm sorry the class library project type this is the um the second product type you learn in the c sharp master class for magic course so in the c-sharp matching course we start off with a console project because it is a user interface it does make life easier for you know for getting information and learning c sharp code but we're not using it as a user interface to learn the ui we're learning it as a user interface to call c sharp code that we're learning we focus in on c sharp code and for the first i don't know a few dozen lessons we only focus on c sharp code itself and not user interface specific code we just use a console you obviously the quickest and easiest but then as soon as you start learning other project types the very next project type you learn is the class library because it's that important you should always almost always be using a class library with your project it is that important i do think that the very least you separate out your non-ui code into a class library so with that if you have any questions let me know down in the comments i do want to point out that this is an intro to class libraries and here's where it fits in the overall hierarchy i've created this csharpprojects.com website that shows you where things fit in their in their categories so desktop web service and other we've got class library this is where this fits but it is like i said probably the most important project type to learn it's the second one you should learn it's the one right behind console just because you're that's how you can call c sharp code easily but class library notice the coming swing video that will replace with this video as soon as it's up but it's important to know this but then once you know this well then it can be called by pretty much any one of these project types so pretty much anything else can call a class library so worker servers can console can api razor pages nbc plays a server place webassembly grpc azure functions they can all call class libraries winforms wpf uwp dot net maui blazer hybrid console they can all talk to the class library and yes they have console on more than one spot and that's intentional but um that's where it fits in the hierarchy is it's a kind of a it's not a project that you build on its own usually usually you build this project in support of another project in fact you always build it in support of something else so the class library seems like it's kind of like this you know little thing that maybe you'll learn someday no no learn this first use it first and then use it with all these other project types okay so that's where it fits in the hierarchy i encourage you to use this tool to now you know this now you can pick what to learn next and say okay i know how to use a class library i've practiced it i've tried a few times now i want to integrate it into the the ui i'm already using or maybe i'll learn a new ui and but then also use that same class library so with that that's the class library project if you have any questions leave them down the comments i'll try to get to as many as possible i can't get to all of them but i'll try to get as many as possible thanks for watching as always i am tim cory [Music] [Applause] you
Info
Channel: IAmTimCorey
Views: 65,114
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# course, C# training, C# tutorial, .net core, vs2022, .net 6, c# tutorial for beginners
Id: C6LV_xMGdKc
Channel Id: undefined
Length: 61min 36sec (3696 seconds)
Published: Mon Aug 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.